Sean's Note: [讀書筆記] Effective Objective-C - Chapter 2

2014年12月22日 星期一

[讀書筆記] Effective Objective-C - Chapter 2

2. Objects, Messaging, and the Runtime

Item 6: Understand Properties

  1. Four categories of attributes can be applied: 
    1. Atomicity: atomic(default)/nonatomic
    2. Read/Write: readwrite/readonly
    3. Memory-Management Semantics: assign, strong, weak, unsafe_unretained, copy
    4. Methods Names: getter=<name>, setter=<name>
  2. If you implement your own accessors, you should make them adhere to specified attributes yourself.
  3. Never use use your own accessors in an init(or dealloc) method.
  4. All properties are declared nonatomic on iOS because the locking introduces such an overhead on iOS, and it doesn't ensure thread safety.

Item 7: Access Instance Variables Primarily Directly When Accessing Them Internally

  1. Prefer to read data directly through instance variables internally and to write data through properties internally. 
Additional Study: Should I Use a Property or an Instance Variable?

Item 8: Understand Object Equality

  1. Use isEqualToString: is faster than calling isEqual:.
  2. Objects that are equal must have the same hash, but objects that have the same hash do not necessarily have to be equal.
  3. Be aware of what can happen when you mutate on object that's in a collection.

Item 9: Use the Class Cluster Pattern to Hide Implementation Detail

  1. Factory pattern is one way of creating a class cluster.
  2. There is no abstract base class for Objective-C. Instead, convention foe how to use a class should be made in documentation.
  3. Most of collection classes are class clusters, such as NSArray and NSMutableArray.
  4. Should use isKindOfClass: rather than class when comparing class cluster.

Item 10: Use Associated Objects to Attach Custom Data to Existing Classes

  1. objc_setAssociatedObject, objc_getAssociatedObject
  2. The accessing of associated objects is functionally similar to imaging that the object is an NSDictionary. An import difference is that key is treated purely as an opaque pointer. Thus, it's common to use static global variables for the keys.
  3. Can be used on delegate protocol methods.
  4. Can easily introduce hard-to-find bugs. 

Item 11: Understand the Role of objc_msgSend
  1. void objc_msgSend(id self, SEL cmd, ...)
  2. The function loos through the list of methods implemented by the receiver's class. It caches the result in a fast map, one for each class.

Item 12: Understand Message Forwarding

  1. It's not compile-time error to send a message to a class that it doesn't understand, since methods can be added to classes at runtime. When it receives a method that it doesn't understand, an object goes through message forwarding.
  2. The forwarding pathways are split into two avenues:
    Dynamic Method Resolution
    +(BOOL)resolveInstanceMethod:(SEL)selector
    +(BOOL)resolveClassMethod:(SEL)selector

    Replacement Receiver
    -(id)forwardingTargetForSelector:(SEL)selector
    Full Forwarding Mechanism
    -(void)forwardInvocation:(NSInvocation*)invocaiton

Item 13: Consider Method Swizzling to Debug Opaque Methods

  1. Swizzling is the process of swapping one method implementation for another, usually to add functionality to the original implementation. 
  2. class_getInstanceMethod, method_exchangeImplementaitons
  3. Useful for debugging.

Item 14: Understand What a Class Object Is

  1. Always prefer introspection methods were possible, rather than direct comparison of class objects, since the object may implement message forwarding.

 

沒有留言:

張貼留言