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 where Key: StringProtocol { | |
func `as`<Decoded: Decodable>(type: Decoded.Type) -> Decoded? { | |
return (try? JSONSerialization.data(withJSONObject: self, options: [])) | |
.flatMap{ try? JSONDecoder().decode(Decoded.self, from: $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
protocol JSONIntitalizable { | |
init?( json: [String: Any]) | |
} | |
extension JSONIntitalizable where Self: Decodable{ | |
init?( json: [String: Any]) { | |
self = (try? JSONSerialization.data(withJSONObject: json, options: [])) | |
.flatMap{ try? JSONDecoder().decode(Self.self, from: $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
import Foundation | |
struct Test: Codable { | |
let items: [HeterogeneousType] | |
} | |
struct HeterogeneousType: Codable { | |
let id: ID | |
var sample: Data { | |
return """ |
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
@discardableResult public func with<T>(_ item: T, update: (inout T) throws -> Void) rethrows -> T { | |
var copy = item | |
try update(©) | |
return copy | |
} | |
@inlinable | |
@discardableResult | |
public func with<T>( _ value: consuming T, update: (inout T) throws -> Void) rethrows -> T { | |
var copy = consume value | |
try update(©) |
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 ObjectiveC | |
public protocol DynamicProperties: class { | |
subscript<T>(dynamic key: String) -> T? { get set } | |
} | |
private extension String { | |
var unsafePointer: UnsafeRawPointer { | |
return UnsafeRawPointer(bitPattern: hashValue)! | |
} |
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 AnyEquatableKeyPath<Root> { | |
let keyPath: PartialKeyPath<Root> | |
let isEqual: (Root, Root) -> Bool | |
init<Value: Equatable>(_ keyPath: KeyPath<Root, Value>) { | |
self.keyPath = keyPath | |
self.isEqual = {left, right in | |
return left[keyPath: keyPath] == right[keyPath: keyPath] | |
} | |
} | |
} |
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 UIResponder { | |
func firstParent<T: UIResponder>(ofType type: T.Type ) -> T? { | |
return next as? T ?? next.flatMap { $0.firstParent(ofType: type) } | |
} | |
} |
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 UIKit | |
extension UIImage { | |
func resize(to size: CGSize) -> UIImage { | |
return UIGraphicsImageRenderer(size: size).image { context in | |
self.draw(in: CGRect(origin: .zero, size: size)) | |
} | |
} | |
func resizeAndCrop(to square: CGFloat)-> UIImage { | |
let targetSize = CGSize(width: square, height: square) |
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 pixel(in image: UIImage, at point: CGPoint) -> (UInt8, UInt8, UInt8, UInt8)? { | |
let width = Int(image.size.width) | |
let height = Int(image.size.height) | |
let x = Int(point.x) | |
let y = Int(point.y) | |
guard x < width && y < height else { | |
return nil | |
} | |
guard let cfData:CFData = image.cgImage?.dataProvider?.data, let pointer = CFDataGetBytePtr(cfData) else { | |
return nil |
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 UIKit | |
import PlaygroundSupport | |
//MARK: Networking | |
protocol NetworkServiceProtocol { | |
func test() | |
} | |
struct NetworkService: NetworkServiceProtocol { | |
func test() { |
OlderNewer