December 2018
Intermediate to advanced
414 pages
10h 19m
English
In Objective-C, we may want to initialize some properties lazily as they may only be accessed at a future time, we don't want to block the init() call with a long-running operation, or when a required instance property will only be accessible at a later time.
In Swift, we are blessed by the presence of the lazy keyword.
Let's start with this code and see how we can improve it:
class MyView: NSView { let image: NSImage! let imageView = NSImageView(frame: .zero) init(imageName: NSImage.Name, frame: NSRect) { image = NSImage(named: imageName) super.init(frame: frame) } func setup() { addSubview(imageView) imageView.frame = bounds.insetBy(dx: 5.0, dy: 5.0) imageView.image = image imageView.imageAlignment = .alignCenter } ...
Read now
Unlock full access