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.
Tag: cs
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
When to use weak variable
Object A holds a reference to Object B while Object B also holds a reference to Object A, then we have a retain cycle here. To break it, one of the objects should be defined as weak. So the ARC can eventually count down to zero then release both objects.
UIScrollView performance
Load the subviews in the scroll view only when they need to be displayed. Similar to how UITableView works.
Background: colorWithPatternImage vs. UIImageView
For small image repeated/tiled, use colorWithPatternImage
For large full-size image, use UIImageView
Higher order functions
What? Functions you can pass as arguments to other functions. Such as map, filter, reduce, flatMap.
- map – operate on each element in the collection (then return a new collection).
- filter – return a subset of the collection that satisfies some condition.
- reduce – combine and return a single value.
- flatMap – flatten a collection of collections into a collection.
What slows down table scroll
- cell creation
- transparency
- gradient
- shadow
- off-screen drawing
- image scaling
- row height calculation
- api call
- data transformation
- deep subviews