Opaque means no transparency defined. If not set, it will impact performance, especially the animated UIs.
Why? iOS needs to go deeper into the view hierarchy to figure out what color to render.
How? Use Debug\Color Blended Layers option to locate non-opaque views.
Category: Reading Notes
NSError
Notes from NSError
domain: the subsystemcode: error code within that subsystemuserInfo: a dictionary, contains all other details
Also available: localizedDescription
Defer keyword
Run the block of code before leaving the current scope. Similar to “finally” in other language’s try/catch block.
KVO
Notify objects when changes to properties of other objects. Very similar to willSet and didSet, but they can be defined outside of the type defination.
observation = observe(
\.objectToObserve.myDate,
options: [.old, .new]
) { object, change in
print("myDate changed from: \(change.oldValue!), updated to: \(change.newValue!)")
}
Never block main thread
Why? UI rendering (UIKit) works in the main thread.
How? All heavy lifting (time-consuming) should be moved out of main thread, including:
- Disk I/O
- Networking, e.g., API calls
- Large computations
- Use GCD, and/or Operation
Responder chain
UIControl actions will send events to a chain of responders, if the 1st one doesn’t implement the action, then it goes deeper to the 2nd one, till it’s handled or no more responders in the chain.
Dynamic dispatch
What? Since override is support by Swift, it has to be determined at runtime what methods/properties to call, so an indirect call cannot be avoid.
When performance is important, to minimize and help compiler optimization, use private, final access levels to declare methods/properties.
Don’t keep instantiating heavy objects
What? Classes like NSDateFormatter, NSCalendar are heavy to instantiate.
How? Create singleton (and it should be thread-safe if the singleton is created properly.)
Tips: If possible, use UNIX epoch (an Int) to represent a date. That speeds up the date object creation (vs. creation by parsing date formatted string.)
bounds vs. frame
- bounds: uses its own coordinate system, used to place views in itself.
- frame: uses parent view’s coordinate system, used to place it in the parent view.
Persistent local storage options
- NSUserDefaults – a tiny piece of info
- JSON files, NSCoding – large one time data, when used, need to first load from disk to memory. And entire payload has to be loaded before being used.
- SQLite, Core Data – large queryable data