Skip to content

Instantly share code, notes, and snippets.

@aryaxt
aryaxt / Swift Identifiable protocol
Created November 1, 2016 04:20
Swift Identifiable protocol
// 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
}
@aryaxt
aryaxt / Swift auto object serialization
Last active November 4, 2016 19:15
Swift auto object serialization
public protocol Serializable {
func serialize() -> [String: Any]
}
extension Serializable {
public func serialize() -> [String: Any] {
return Self.makeSerialized(serializable: self)
}
@aryaxt
aryaxt / KeyboardManager
Created August 21, 2016 22:08
KeyboardManager
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
@aryaxt
aryaxt / RawRepresentableExtension
Last active April 19, 2018 08:31
Swift enum -> Get an array of all cases
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 }
@aryaxt
aryaxt / Parse Layer
Created December 4, 2015 05:58
A set of classes/extensions to abstract the parse logic and also using a Result enum to pass data around
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))
}
@aryaxt
aryaxt / UIView+Extension
Created November 28, 2015 21:52
UIView+Extension
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
}
@aryaxt
aryaxt / Multiple Storyboard Usage.txt
Last active December 17, 2015 02:05
Multiple Storyboard Usage
@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) {
@aryaxt
aryaxt / Swift SequenceType GroupBy
Last active February 4, 2020 13:55
Swift Array GroupBy
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)
@aryaxt
aryaxt / Swift Multi threading operator
Last active October 14, 2017 08:37
Swift Multi threading operator
// 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
@aryaxt
aryaxt / Int Extension execution loop
Last active August 29, 2015 14:06
Int extension - Execute a block based on given number of times
extension Int {
public func execute (closure: () -> Void) {
for index in 1...self {
closure()
}
}
}
// Usage
10.execute { println("print times") }