目录
- 1 The System Framework
- Item 47:Familiarize Yourself with the System Frameworks
- Item 48:Prefer Block Enumeration to for loops
- Item 49:Use Toll-Free Bridging for Collections with Custom Memory-Management Semantics
- Item 50:Use NSCache Instead of NSDictionary for Caches
- Item 51:Keep initialize and load Implementations Lean
- Item 52:Remember that NSTimer Retains Its Target
- 2 Reference
1 The System Framework
Item 47:Familiarize Yourself with the System Frameworks
Item 48:Prefer Block Enumeration to for loops
-
Enumerating collections can be achieved in four ways. The for loop is the most basic, followed by enumeration using
NSEnumerator
and fast enumeration. The most modern and advanced way is using the block-enumeration methods. -
Block enumeration alows you to perform the enumeration concurrently, withou any additional code, by making use of GCD. This cannot easily be achieved with the other enumeration techniques.
-
Alter the block signature to indicate the precise types of objects if you know them.
Item 49:Use Toll-Free Bridging for Collections with Custom Memory-Management Semantics
-
Toll-free bridging allows you to cast between Foundation’s Objective-C objects and CoreFoundation’s C data structures.
-
Dropping down to CoreFoundation to creat a collection allows you to specify various callbacks that are used when the colleciton handles its contents. Casting this through toll-free bridging allows you to end up with an Objective-C collection that has custom memory-management semantics.
Item 50:Use NSCache Instead of NSDictionary for Caches
-
Consider using
NSCache
in the place ofNSDictionary
objects being used as caches. Caches provide optimal pruning behavior, thread safety, and don’t copy keys, unlike a dictionary. -
Use the count limite and cost limits to manipulate the metrics that define when objects are pruned from the cache. But never rely on those metrics to be hard limits; they are purely guidance for the cache.
3.Use NSPurgeableData
objects with a cache to provide autopurging data that is also automatically removed from the cache when purged.
- Caches will make your applications more responsive if used correctly. Cache only data that is expensive to recalculate, such as data that needs to be fetched fromt he network or read from disk.
Item 51:Keep initialize and load Implementations Lean
-
Classes go through a load phase in which they have the
load
method called on them if it has been implemented. This method may also be present in categories whereby the class load always happens before the category load. Unlike other mehtods, theload
method does not participate in overriding. -
Before a class is used for the first time, it is sent the
initialize
method. This method does participate in overriding, so it is usually best tocheck which class is being initialized. -
Both implementations of
load
andinitialize
should be kept lean, which helps to keep applications responsive and reduces the likelihood that interdependency cycles will be introduced. -
Keep
initialize
methods for setting up global state that cannot be done at compile time.
Item 52:Remember that NSTimer Retains Its Target
-
An
NSTimer
object retains its target until the time is invalidated either because it fires or through an explicit call to invalidate. -
Retain cycles are easy to introduce through the use of repeating timers and do so if the target of a timer retains the timer. This may happen directly or indirectly through other objects in the object graph.
-
An extension to
NSTimer
to use blocks can be used to break the retain cycle. Until this is made part of the publicNSTimer
interface, the functionality must be added through a category.