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 Wrapper { | |
associatedtype Value | |
var value: Value { get set } | |
init(value: Value) | |
init(_ value: Value) | |
func unwrap() -> Value | |
} | |
extension Wrapper { | |
init(_ value: 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
struct InoutIUO { | |
static func f<T>(_: inout T) { | |
print("f<T>(T)") | |
} | |
static func f<T>(_: inout T?) { | |
print("f<T>(T?)") | |
} | |
static func f<T>(_: inout Array<T>?) { |
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
class ObservationToken { | |
let cancellationClosure: (ObservationToken) -> Void | |
init(cancellationClosure: @escaping (ObservationToken) -> Void) { | |
self.cancellationClosure = cancellationClosure | |
} | |
func cancel() { | |
cancellationClosure(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
protocol StaticDecodable { | |
static func decode(from decoder: Decoder) throws -> Self | |
} | |
// MARK: - Container Extensions | |
extension KeyedDecodingContainer { | |
func decode<T>(_ type: T.Type, forKey key: K) throws -> T where T: StaticDecodable { | |
return try T.decode(from: try self.superDecoder(forKey: key)) |
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 Dictionary { | |
init<S>(grouping values: S, by kp: KeyPath<S.Element, Key>) throws where Value == [S.Element], S: Sequence { | |
try self.init(grouping: values, by: { $0[keyPath: kp] }) | |
} | |
} | |
// Crashes: | |
// extension Dictionary { | |
// init<S, K>(grouping values: S, by kp: K) throws where Value == [S.Element], S: Sequence, K: KeyPath<S.Element, Key> { | |
// try self.init(grouping: values, by: { $0[keyPath: kp] }) |
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 map<K, R>(keyPath kp: K) -> [R] where K: KeyPath<Element, R> { | |
return self.map { $0[keyPath: kp] } | |
} | |
} |
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
find . -name '*@2x.png' -exec identify -format "%f %[width]x%[height]\n" {} \; | grep -v 90x90 |
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
// traits | |
protocol TableViewUpdating { | |
func update(at: IndexPath) | |
} | |
extension TableViewUpdating { | |
func update(at indexPath: IndexPath) { | |
// default implementation | |
} |
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 TrackProvider { | |
var track: Track? { get } | |
func observeTrack(_ block: (TrackProvider, NSKeyValueObservedChange<Track>)) -> NSKeyValueObservation | |
} | |
extension TrackProvider where Self: NSObject { | |
func observeTrack(_ block: (TrackProvider, NSKeyValueObservedChange<Track?>)) -> NSKeyValueObservation { | |
return self.observe(\TrackProvider.track) { obj, change in | |
} |
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 Foundation | |
class KVObserver: NSObject { | |
let subject: NSObject | |
let keyPath: String | |
let block: (Any?) -> Void | |
init(subject: NSObject, keyPath: String, block: @escaping (Any?) -> Void) { | |
self.subject = subject | |
self.keyPath = keyPath |