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
// This allows you to implement identifiable on any class/struct and automatially make them Equatable | |
protocol Identifiable: Equatable { | |
var id: Int { get } | |
} | |
func == <T: Identifiable>(lhs: T, rhs: T) -> Bool { | |
return lhs.id == rhs.id | |
} |
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 protocol Serializable { | |
func serialize() -> [String: Any] | |
} | |
extension Serializable { | |
public func serialize() -> [String: Any] { | |
return Self.makeSerialized(serializable: self) | |
} | |
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 KeyboardManager { | |
public struct KeyboardChange { | |
public let visibleHeight: CGFloat | |
public let endFrame: CGRect | |
public let animationDuration: TimeInterval | |
public let animationOptions: UIViewAnimationOptions | |
} | |
public typealias KeyboardChangeClosure = (KeyboardChange)->Void |
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 } |
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
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
@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
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
// 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 Int { | |
public func execute (closure: () -> Void) { | |
for index in 1...self { | |
closure() | |
} | |
} | |
} | |
// Usage | |
10.execute { println("print times") } |
NewerOlder