Skip to content

Instantly share code, notes, and snippets.

View zmcartor's full-sized avatar

Zach zmcartor

View GitHub Profile
@zmcartor
zmcartor / dynamic-programming.swift
Created May 25, 2018 16:11
Dynamic Programming with field of spikes in Swift
let field = [0,0,0,0,1,0,0,0,1,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,1,1]
var memo:[Int:[Int:Bool]] = [0:[:]]
func bounce(startingSpeed:Int, startingIndex:Int, field:[Int]) -> Bool {
// Many base cases. We must still be on the runway.
// We must land on a place with 0 (no spikes)
// Use a double dictionary as the memo. Store true/false. 1 dimension for each changing intput!!!!
@zmcartor
zmcartor / GenericStack.swift
Created October 30, 2018 03:27
Swift Network Stack
enum Result<T> {
case success(T)
case error(Error)
}
struct ToDo : Codable {
var id: Int
var userId: Int
var title:String
var completed:Bool
@zmcartor
zmcartor / UIImageView+loadFromURL-extension.swift
Last active November 12, 2018 23:09
UIImageView load from URL, placeholder and cache
extension UIImageView {
func setImage(from url: URL, withPlaceholder placeholder: UIImage? = .none, withCache cache:NSCache<NSURL,UIImage>? = .none) -> URLSessionDataTask? {
if let cached = cache?.object(forKey: url as NSURL) {
self.image = cached
return .none
}
self.image = placeholder
let task = URLSession.shared.dataTask(with: url) { (data, _, _) in
@zmcartor
zmcartor / OSLog+Extensions.swift
Created March 18, 2019 18:13
OSLog extension for os_signpost usage
/* Usage
let log = OSLog(subsystem: "deep-secrets", category: "cookie-recipes")
let signpostID = log.beginSignpost(withName: "signpost name")
... Do work ...
log.endSignpost(withName: "signpost name" , signpostId: signpostId)
Signposts can also be started w objects instead of SignpostIDs when scope is an issue
@zmcartor
zmcartor / mvvmc.swift
Created August 19, 2021 03:16
MVVM+Coordinator Architecture
import UIKit
struct FrontScreenViewmodel {
var hello = "there"
}
// Builds the ViewControllers, and injects the ViewModels
class Factory {
let viewModelFactory = ViewModelFactory()