Skip to content

Instantly share code, notes, and snippets.

View jayrhynas's full-sized avatar

Jayson Rhynas jayrhynas

View GitHub Profile
protocol Wrapper {
associatedtype Value
var value: Value { get set }
init(value: Value)
init(_ value: Value)
func unwrap() -> Value
}
extension Wrapper {
init(_ value: Value) {
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>?) {
class ObservationToken {
let cancellationClosure: (ObservationToken) -> Void
init(cancellationClosure: @escaping (ObservationToken) -> Void) {
self.cancellationClosure = cancellationClosure
}
func cancel() {
cancellationClosure(self)
}
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))
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] })
extension Array {
func map<K, R>(keyPath kp: K) -> [R] where K: KeyPath<Element, R> {
return self.map { $0[keyPath: kp] }
}
}
find . -name '*@2x.png' -exec identify -format "%f %[width]x%[height]\n" {} \; | grep -v 90x90
// traits
protocol TableViewUpdating {
func update(at: IndexPath)
}
extension TableViewUpdating {
func update(at indexPath: IndexPath) {
// default implementation
}
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
}
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