Skip to content

Instantly share code, notes, and snippets.

@joshuajhomann
joshuajhomann / Dictionary as Decodable
Last active July 1, 2018 16:32
Extend Dictionary to convert to Decodable
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) }
}
}
@joshuajhomann
joshuajhomann / JSONIntitalizable
Last active June 19, 2019 19:28
JSONIntitalizable
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) }!
}
}
import Foundation
struct Test: Codable {
let items: [HeterogeneousType]
}
struct HeterogeneousType: Codable {
let id: ID
var sample: Data {
return """
@joshuajhomann
joshuajhomann / with.swift
Last active February 24, 2024 05:10
With
@discardableResult public func with<T>(_ item: T, update: (inout T) throws -> Void) rethrows -> T {
var copy = item
try update(&copy)
return copy
}
@inlinable
@discardableResult
public func with<T>( _ value: consuming T, update: (inout T) throws -> Void) rethrows -> T {
var copy = consume value
try update(&copy)
import ObjectiveC
public protocol DynamicProperties: class {
subscript<T>(dynamic key: String) -> T? { get set }
}
private extension String {
var unsafePointer: UnsafeRawPointer {
return UnsafeRawPointer(bitPattern: hashValue)!
}
@joshuajhomann
joshuajhomann / equatableKeyPaths
Created February 25, 2019 03:16
Equatable Key Paths
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]
}
}
}
@joshuajhomann
joshuajhomann / firstParent
Created March 15, 2019 15:39
firstParent
extension UIResponder {
func firstParent<T: UIResponder>(ofType type: T.Type ) -> T? {
return next as? T ?? next.flatMap { $0.firstParent(ofType: type) }
}
}
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)
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
@joshuajhomann
joshuajhomann / Dependency Injection by Protocol Extension
Created May 11, 2019 17:33
Dependency Injection by Protocol Extension
import UIKit
import PlaygroundSupport
//MARK: Networking
protocol NetworkServiceProtocol {
func test()
}
struct NetworkService: NetworkServiceProtocol {
func test() {