Nathan Lamont

Notes to Self

Swift Resources & Notes

2021-02-17

A little out of date but great conceptually:

Related site

Caching computed properties

Swift has computed properties; here's a pattern for caching them:

struct Cached <T, U> {
    var value : T { willSet { _calculated = nil } }
    private var _calculated : U? = nil
    private var function : (T) -> U
    
    init(_ value: T, _ f: (T) -> U) {
        self.function = f
        self.value = value
    }
    
    var calculated : U {
        mutating get {
            if _calculated == nil { _calculated = function(value) }
            return _calculated!
        }
    }
}

Example use:

var a = Cached(0) {
    (a: Int) -> Int in
    print("did calculate")
    return a + 100
}
// Prints "did calculate"

print(a.calculated)
// Prints "100"

a.value = 10

print(a.calculated)
// Prints "did calculate\n110"

Nice tips on use language features to make more expressive, explicit code

https://www.swiftbysundell.com/articles/what-makes-code-swifty/ via https://news.ycombinator.com/item?id=22399082

Good discussion on current state of SwiftUI as of 2020.11.22

https://news.ycombinator.com/item?id=25171532 (really about https://triplebyte.com/blog/should-i-use-swiftui-in-production-heres-how-to-decide?ref=hnpost which calls out a lot of the current shortcomings but still loves it)

Hash Tables

Promising? 2 years old

Another article on hashing

But there is some built-in "hashable" protocol. From 2020 This appears to be for storing items in a dictionary.

Stanford course covering SwiftUI

Swift in Depth

Badly written article about vision framework/OCR. https://www.shawnbaek.com/posts/lets-make-a-receipt-text-recognizer-with-the-apple-vision-framework

https://github.com/STREGAsGate/GameMath Handy math https://www.reddit.com/r/swift/comments/mu6fpn/gamemath_for_swift_is_now_open_source/?utm_source=share&utm_medium=ios_app&utm_name=iossmf

Code Examples

Some guy's "game math" - from above, is for 3d. Not optimized, but maybe a relevant example of an approach to stuff you are interested in.