See Apple release note
Author: Weiran
Live Rendering
Notes from IBInspectable / IBDesignable
user-defined runtime attributes: allows updating attribute value in Xcode
IBInspectable: easlier editable in Xcode
Can add inspectable property to existing classes via extension.
IBDesignable: WYSIWYG.
Use prepareForInterfaceBuilder() to provide dummy data at design time. It’s not run in shipping code.
Escaping closure
A clusure that is
- passed in to a function as parameter
- called after the function has returned
Closures are non-escaping by default in Swift 3+.
Use @escaping to mark it as escaping.
What slows down app launch
- api calls
- disk I/O
- data preparation
let vs. read-only (computed)
letmeans constant, and cannot be set after initialized.- read-only also means cannot be set but since it is computed so it could return different value at different times.
2 Swift examples of Decorator Design Pattern
Extension and Delegation
QoS
- User-interactive: UI feedback. Such as animation. (<1s)
- User-initiated: UI works that required in order to continue the interaction. Such as opening a file. (1~3s)
- Utility: UI works that have no need for an immediate result. Such as downloading a file. (3s~3m)
- Background: Non-UI works. Such as indexing, backup. (3m~3h)
Location tracking and battery usage
- Visits location service (CLVisit): most power-efficient way. Not for navigation or real-time. Good for doing async background processing related to location. Or to identify location pattern. Requires Always authorization.
- Region monitoring (Geofencing): Define a circular region and your app gets a notification when crossing the boundary. Apple’s built-in Reminder app uses that too. Very little battery consumption. It is built upon Core Location’s significant-change location service. Requires Always authorization.
- Significant-change location service: power-friendly way. Requires Always authorization.
- Standard location service: Real-time. Most power consumption.
When/what/how to cache
When? Read a lot, but no/min updates.
What? Images, API call results, computational results (e.g., row heights).
How? Use NSCache, networking frameworks that supports caching, set cache policy (e.g., NSURLRequest.CachePolicy.returnCacheDataElseLoad).
inout keyword
Use it when you need to pass the reference to a value variable (vs. just the value) into a function.