Skip to content

Instantly share code, notes, and snippets.

View bnickel's full-sized avatar

Brian Nickel bnickel

View GitHub Profile
@bnickel
bnickel / myFilter
Created June 11, 2014 16:53
Example filter implementation in Swift.
import Foundation
struct FilteringGenerator<T where T:Generator> : Generator {
let test:(T.Element) -> Bool
var generator:T
init(_ generator:T, _ test: (T.Element) -> Bool) {
self.test = test
self.generator = generator
}
@bnickel
bnickel / PrimeEnumerator.m
Last active August 29, 2015 14:02
Infinite prime sequences in Objective-C and Swift
@import Foundation;
@interface PrimeEnumerator : NSObject<NSFastEnumeration>
- (NSArray *)knownPrimes;
@end
@interface PrimeEnumerator ()
@property (nonatomic, strong) NSMutableArray *primes;
@end
// Good idea or Bad idea?
// Lets say you have to call some API with lots of parameters that you don't control
// Like this http://developer.android.com/reference/android/text/StaticLayout.html#StaticLayout(java.lang.CharSequence, int, int, android.text.TextPaint, int, android.text.Layout.Alignment, float, float, boolean, android.text.TextUtils.TruncateAt, int)
// Do you prefer Option #1 or Option #2? Is Option #2 a terrible idea?
// I suppose Option #3 could be to wrap it in a Builder
class Foo {
@bnickel
bnickel / ConstraintPlayground.swift
Created September 15, 2014 17:28
Shorthand declaration of NSLayoutConstraint without visual format. (For those one-off cases where you don't want to use the visual format language.)
import Cocoa
struct PartialConstraint {
private let item:AnyObject
private let attribute:NSLayoutAttribute
private let multiplier:CGFloat?
private let constant:CGFloat?
private func constraintWith(partial:PartialConstraint, relation:NSLayoutRelation) -> NSLayoutConstraint {
assert(multiplier == nil && constant == nil, "Cannot define multiplier or constant on LHS.")
@bnickel
bnickel / SEUILeftInsetFromRightButton.swift
Created October 15, 2014 18:33
A simple button where the left inset is adjusted to a fixed value, pinning the content to the right edge. Used in Stack Exchange.app to have a variable width empty button with an icon on the right side. Uses IBDesignable and IBInspectable so we never have to reference the button in code. :)
import UIKit
@IBDesignable
class SEUILeftInsetFromRightButton: UIButton {
@IBInspectable var leftInsetFromRight:CGFloat = 0 {
didSet {
updateContentEdgeInsetsWithWidth(CGRectGetWidth(bounds))
}
}
@bnickel
bnickel / fix-xcode
Last active August 29, 2015 14:08 — forked from rnapier/fix-xcode
#!/usr/bin/python
# fix-xcode
# Rob Napier <robnapier@gmail.com>
# Script to link in all your old SDKs every time you upgrade Xcode
# Create a directory called /SDKs (or modify source_path).
# Under it, put all the platform directories:
# MacOSX.platform iPhoneOS.platform iPhoneSimulator.platform
# Under those, store the SDKs:
@bnickel
bnickel / bad-error-messages
Created December 26, 2014 19:39
Demonstrates NSURLSession's lack of localized error descriptions.
#!/usr/bin/swift
import Foundation
let url = NSURL(string: "http://not-a-real-host")!
let request = NSURLRequest(URL: url)
var response:NSURLResponse? = nil
var error:NSError? = nil
NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)
@bnickel
bnickel / page.html
Created January 12, 2015 21:21
Test page to see how far away from a link you can touch while still tapping it.
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
<script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
<style>
div {
position: absolute;
background-color:green;
left: 100px;
}
@bnickel
bnickel / command-line.swift
Created January 21, 2015 17:59
Provides operators ans wrappers for terse command execution in Swift scripts.
#!/usr/bin/swift
// TL;DR: Skip to line 183.
// To run as a script, run with /usr/bin/swift or chmod +x.
// To run as a playground, comment out the first line and `mkdir -p "~/Documents/Shared Playground Data"`.
import Foundation
let stdin = NSFileHandle.fileHandleWithStandardInput()
let stdout = NSFileHandle.fileHandleWithStandardOutput()
@bnickel
bnickel / aspect-grow.swift
Created August 7, 2015 15:28
Walgreens photo uploaded would only let me crop, but I had a bunch of square photos, so...
#!/usr/bin/swift
// Because sometimes you don't want to crop.
// USAGE: aspect-grow RATIO FILES...
// aspect-grow 4:6 *.jpg
//
// Places all files in an output directory relative to their location, so
// aspect-grow 4:6 *.jpg creates a folder "output" in this directory but
// aspect-grow 4:6 a/b.jpg and b/c.jpg creates folders a/output and b/output.