This file contains 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 Sequence { | |
func categorise<U : Hashable>(_ key: (Iterator.Element) -> U) -> [U:Int] { | |
var dict: [U:Int] = [:] | |
self.forEach { el in | |
let key = key(el) | |
dict[key] = (dict[key] ?? 0) + 1 | |
} | |
return dict |
This file contains 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 CGRect { | |
var center: CGPoint { | |
return CGPoint(x: midX, y: midY) | |
} | |
} |
This file contains 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 String { | |
subscript (i: Int) -> Character { | |
return self[self.startIndex.advancedBy(i)] | |
} | |
subscript (i: Int) -> String { | |
return String(self[i] as Character) | |
} |
This file contains 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
enum StatusCode: Int { | |
// Informational | |
case Continue = 100 | |
case SwitchingProtocols = 101 | |
case Processing = 102 | |
// Success | |
case OK = 200 | |
case Created = 201 | |
case Accepted = 202 |
This file contains 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 UIEdgeInsets { | |
init(inset: CGFloat) { | |
self.top = inset | |
self.bottom = inset | |
self.left = inset | |
self.right = inset | |
} | |
} |
This file contains 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 Array { | |
func randomValue() -> Element? { | |
guard self.isEmpty == false else { | |
return nil | |
} | |
let idx = Int(arc4random() % UInt32(count)) | |
return self[idx] | |
} | |
} |
This file contains 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 func <(a: NSDate, b: NSDate) -> Bool { | |
return a.compare(b) == NSComparisonResult.OrderedAscending | |
} | |
public func >(a: NSDate, b: NSDate) -> Bool { | |
return a.compare(b) == NSComparisonResult.OrderedDescending | |
} | |
This file contains 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
import CoreTelephony | |
let info: CTTelephonyNetworkInfo = CTTelephonyNetworkInfo() | |
guard let carrier: CTCarrier = info.subscriberCellularProvider else { | |
// No carrier info available | |
return | |
} | |
print(carrier.carrierName) | |
print(carrier.mobileCountryCode) |
This file contains 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
#if os(OSX) | |
import AppKit | |
typealias Color = NSColor | |
#elseif os(iOS) || os(tvOS) | |
import UIKit | |
typealias Color = UIColor | |
#endif | |
extension Color |
NewerOlder