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
| infix operator ||= {} | |
| func ||= <T> (first: T?, second: T) -> T { | |
| if let l = first { | |
| return l | |
| } | |
| return second | |
| } |
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
| operator postfix ^ {} | |
| @postfix func ^ <T> (object: T?) -> T? { | |
| #if DEBUG | |
| println(object) | |
| #endif | |
| return object | |
| } |
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
| public class OrderedDictionary <K: Hashable, V> { | |
| typealias Key = K | |
| typealias Value = V | |
| private final var dictionary = [K: V]() | |
| private final var orderedKeys = [K]() | |
| init() { | |
| } |
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 Int { | |
| public func execute (closure: () -> Void) { | |
| for index in 1...self { | |
| closure() | |
| } | |
| } | |
| } | |
| // Usage | |
| 10.execute { println("print times") } |
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
| // Takes 2 closures. | |
| // Executes first closure in background and passes its value to the second closure on the ui thread | |
| infix operator ~> {} | |
| func ~> <T> (first:() -> T?, second:(result: T?) -> Void) -> Void { | |
| dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in | |
| var result: T? = first() | |
| dispatch_async(dispatch_get_main_queue(), { () -> Void in |
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 Sequence { | |
| func groupBy<G: Hashable>(closure: (Iterator.Element)->G) -> [G: [Iterator.Element]] { | |
| var results = [G: Array<Iterator.Element>]() | |
| forEach { | |
| let key = closure($0) | |
| if var array = results[key] { | |
| array.append($0) |
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
| @interface UIStoryboard (Additions) | |
| - (id __nullable)tryInstantiateViewControllerWithIdentifier:(NSString * __nonnull)identifier; | |
| @end | |
| @implementation UIStoryboard (Additions) | |
| - (id)tryInstantiateViewControllerWithIdentifier:(NSString *)identifier { | |
| @try { | |
| return [self instantiateViewControllerWithIdentifier:identifier]; | |
| } | |
| @catch (NSException *exception) { |
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
| public extension UIView { | |
| public class func initializeFromNib() -> Self { | |
| return initializeFromNib(self) | |
| } | |
| public class func initializeFromNib<T: UIView>(type: T.Type) -> T { | |
| return NSBundle.mainBundle().loadNibNamed(String(type), owner: nil, options: nil).first as! T | |
| } | |
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
| public extension PFCloud { | |
| public class func fetchObjectFromCloud<T>(functionName: String, parameters: [NSObject: AnyObject] = [:], completion: NetworkResponse<T>->Void) { | |
| PFCloud.callFunctionInBackground(functionName, withParameters: parameters) { result, error in | |
| if let error = error { | |
| completion(NetworkResponse.Failure(error)) | |
| } | |
| else { | |
| completion(NetworkResponse.Success(result![0] as! T)) | |
| } |
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 RawRepresentable where Self: Hashable { | |
| private static func iterateEnum<T: Hashable>(_: T.Type) -> AnyIterator<T> { | |
| var index = 0 | |
| let closure: () -> T? = { | |
| let next = withUnsafePointer(to: &index) { | |
| $0.withMemoryRebound(to: T.self, capacity: 1) { $0.pointee } | |
| } | |
| guard next.hashValue == index else { return nil } |
OlderNewer