This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func arc4random_uniform<T: SignedIntegerType>(max: T) -> T { | |
| let max32: Int32 = numericCast(max) | |
| return T(Int64(arc4random_uniform(UInt32(max32)))) | |
| } | |
| func arc4random_uniform<T: UnsignedIntegerType>(max: T) -> T { | |
| let max32: UInt32 = numericCast(max) | |
| return T(UInt64(arc4random_uniform(max32))) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // objective-c: | |
| typedef NS_OPTIONS(NSInteger, MesozoicPeriod) { | |
| MesozoicPeriodTriassic, | |
| MesozoicPeriodJurassic, | |
| MesozoicPeriodCretaceous | |
| }; | |
| // Swift | |
| let period: MesozoicPeriod = MesozoicPeriod.Jurassic | |
| let period2 = MesozoicPeriod.Jurassic | MesozoicPeriod.Cretaceous |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Open-ended range operators | |
| // | |
| // 100... is equivalent to 100...Int.max | |
| // ...-100 is equivalent to Int.min...-100 | |
| // ..<3 is equivalent to Int.min..<3 | |
| import Swift | |
| /// Conforming types provide static `max` and `min` constants. | |
| protocol MinMaxType { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Curry Recipe | |
| func curryRecipe(n: Int) -> String { | |
| let types = join(", ", map(1...n, { "T\($0)" })) | |
| let returnType = join(" -> ", map(1...n, { "T\($0)" })) | |
| let closures = join(" in ", map(1...n, { "{ t\($0)" })) | |
| let braces = join(" ", Array(count: n, repeatedValue: "}")) | |
| return "func curry<\(types), R>(f: (\(types)) -> R) -> \(returnType) -> R {\r" + | |
| " return \(closures) in f(\(types.lowercaseString)) \(braces)\r}" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyCell: UITableViewCell { | |
| override init(style: UITableViewCellStyle, reuseIdentifier: String?) { | |
| super.init(style: style, reuseIdentifier: reuseIdentifier) | |
| configureView() | |
| } | |
| required init(coder aDecoder: NSCoder) { | |
| super.init(coder: aDecoder) | |
| configureView() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension NSTimer { | |
| /** | |
| Creates and schedules a one-time `NSTimer` instance. | |
| - Parameters: | |
| - delay: The delay before execution. | |
| - handler: A closure to execute after `delay`. | |
| - Returns: The newly-created `NSTimer` instance. | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // (c) 2014 Nate Cook, licensed under the MIT license | |
| // | |
| // Fisher-Yates shuffle as top-level functions and array extension methods | |
| /// Shuffle the elements of `list`. | |
| func shuffle<C: MutableCollectionType where C.Index == Int>(inout list: C) { | |
| let c = count(list) | |
| for i in 0..<(c - 1) { | |
| let j = Int(arc4random_uniform(UInt32(c - i))) + i | |
| swap(&list[i], &list[j]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // see also https://gist.github.com/griotspeak/8bb4c46611fc90d3043b | |
| func findFirst<S: SequenceType>(seq: S, predicate: S.Generator.Element -> Bool) -> S.Generator.Element? { | |
| for x in seq { | |
| if predicate(x) { return x } | |
| } | |
| return nil | |
| } | |
| func not<T>(predicate: T -> Bool) -> (T -> Bool) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // adadpted from http://atp.fm/t-shirt | |
| import UIKit | |
| class ATPView : UIView { | |
| override func drawRect(rect: CGRect) { | |
| let fontName = "MyriadPro-SemiBold" | |
| let title = NSLocalizedString("Accidental Tech Podcast", comment: "title") | |
| let initials = NSLocalizedString("ATP", comment: "initials") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // (c) 2014 Nate Cook, licensed under the MIT license | |
| extension NSDate { | |
| /// Initializes an NSDate instance with the given date and time parameters. | |
| convenience init(year: Int, month: Int, day: Int, hour: Int, minute: Int = 0, second: Int = 0) { | |
| var components = NSDateComponents() | |
| components.year = year | |
| components.month = month |