How can I add caching to AsyncImage

I had the same problem as you. I solved it by writing a CachedAsyncImage that kept the same API as AsyncImage, so that they could be interchanged easily, also in view of future native cache support in AsyncImage. I made a Swift Package to share it. CachedAsyncImage has the exact same API and behavior as …

Read more

Alternate approach to inheritance for Swift structs?

In Swift with struct you can create protocol for common task and also implement default implementation using protocol extension. protocol Vehicle { var model: String { get set } var color: String { get set } } //Common Implementation using protocol extension extension Vehicle { static func parseVehicleFields(jsonDict: [String:Any]) -> (String, String) { let model …

Read more

Lazy loading Properties in swift

I think a lazy property initialized with a closure would work: lazy var myLabel: UILabel = { var temporaryLabel: UILabel = UILabel() … return temporaryLabel }() As I read “The Swift Programming Language.” (Checkerboard example) the closure is only evaluated once).

What is the difference in Swift between ‘unowned(safe)’ and ‘unowned(unsafe)’?

From what I understand, although I can’t find a definitive source from Apple, unowned can be broken into two flavors, safe and unsafe. A bare unowned is unowned(safe): it is a specially wrapped reference which will throw an exception when a dealloced instance is referenced. The special case is unowned(unsafe): it is the Swift equivalent …

Read more