Skip to content

Instantly share code, notes, and snippets.

@aryaxt
aryaxt / Swift DefaultIfNil operator
Last active August 29, 2015 14:04
Swift default if nil operator
infix operator ||= {}
func ||= <T> (first: T?, second: T) -> T {
if let l = first {
return l
}
return second
}
@aryaxt
aryaxt / Swift debug log operator
Created August 7, 2014 01:12
An operator to log an object description only in debug mode
operator postfix ^ {}
@postfix func ^ <T> (object: T?) -> T? {
#if DEBUG
println(object)
#endif
return object
}
@aryaxt
aryaxt / Swift Ordered Dictionary
Created August 15, 2014 16:20
Swift Ordered Dictionary
public class OrderedDictionary <K: Hashable, V> {
typealias Key = K
typealias Value = V
private final var dictionary = [K: V]()
private final var orderedKeys = [K]()
init() {
}
@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") }
@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 / 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 / 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 / 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 / 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 / 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 }