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

2014年12月11日 星期四

[讀書筆記] Effective Objective-C - Chapter1

1. Accustoming Yourself to Objective-C

Item 1: Familiarize Yourself with Objective-C's Roots

  1. It uses messaging structure with dynamic binding rather than function calling.
  2. Memory for objects is always allocated in heap space and never on the stack.
  3. Some variables like CGRect not holding Objective-C objects are using stack space.

Item 2: Minimize Importing Headers in Headers

  1. Use forward declaration to decrease compile time. EX: @class EOCEmployer;
  2. Using forward declaration also alleviates the problem of both classes referring to each other.

Item 3: Prefer Literal Syntax over the Equivalent Methods

  • Literal Arrays
    NSArray *arrayA = [NSArray arrayWithObjects:object1, object2, object3, nil];NSArray *arrayB = @[object1, object2, object3];
    If object2 is nil, the literal array, array B, will cause the exception to be thrown, However, arrayA will still be created but will contain only object1.
  • Literal Arrays
    Similar to above.
  • Limitations
    If a mutable variant is required, a mutable copy must be taken.
    EX: NSMutableArray *mutable = [@[@1, @2, @3, @4, @5] mutableCopy]; 

Item 4: Prefer Typed Constants to Preprocessor #define

  1. Type constants have type information(More readable).
  2. The usual convention for constants is to prefix with the letter k for constants that are local to a translation unit. For constants that are exposed outside of a class, it's usual to prefix with the class name.

Item 5: Use Enumerations for States, Options, and Status Codes

  1. C++11 brought some changes like the capability to dictate the underlying type used to store variables of the enumerated type.

沒有留言:

張貼留言