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 { | |
subscript(safe index: Int, repeated repeated: Bool) -> Element? { | |
return isEmpty ? nil : self[index % count] | |
} | |
} |
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
protocol Storable {} | |
extension Storable { | |
var storedValue: Any? { | |
get { return UserDefaults.standard["value"] } | |
nonmutating set { UserDefaults.standard["value"] = newValue } | |
} | |
} | |
struct Storage: Storable {} | |
let storage = Storage() | |
storage.storedValue = "🐼" |
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 UIView { | |
func superview<T>(of type: T.Type) -> T? { | |
return superview as? T ?? superview.flatMap { $0.superview(of: T.self) } | |
} | |
} |
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 UInt32 { | |
static func random(_ range: Range<UInt32>) -> UInt32 { | |
return arc4random_uniform(range.upperBound - range.lowerBound) + range.lowerBound | |
} | |
} | |
extension CGFloat { | |
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 where Element: _ArrayType, Element.Generator.Element: AnyObject? { | |
func flatten() { | |
return flatMap { $0.flatMap { $0 } } | |
} | |
} |
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 Optional { | |
func unwrap(_ f: (Wrapped) -> Void) { | |
_ = self.map(f) | |
} | |
} | |
// usage: | |
// let username: String? |
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
@warn_unused_result | |
func Init<T>(value: T, @noescape block: (object: T) -> Void) -> T { | |
block(object: value) | |
return value | |
} |
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
func propertyNames(forClass: AnyClass) -> [String] { | |
var count = UInt32() | |
let properties = class_copyPropertyList(forClass, &count) | |
let names: [String] = (0..<Int(count)).flatMap { i in | |
let property: objc_property_t = properties[i] | |
guard let name = NSString(UTF8String: property_getName(property)) as? String else { | |
debugPrint("Couldn't unwrap property name for \(property)") | |
return nil | |
} | |
return name |
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
// MARK: - CABasicAnimation | |
extension CABasicAnimation { | |
/// Creates a new animation object with its `keyPath` property set to `path` and current `keyPath` value set to `fromValue`. | |
convenience init(keyPath path: String, layer: CALayer) { | |
self.init(keyPath: path) | |
self.fromValue = layer.presentation()?.value(forKeyPath: path) | |
} | |
} |
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 UIFont { | |
func bold() -> UIFont? { | |
return fontDescriptor.withSymbolicTraits(.traitBold).map { UIFont(descriptor: $0, size: 0) } | |
} | |
} |