1. Accustoming Yourself to Objective-C
Item 1: Familiarize Yourself with Objective-C's Roots
- It uses messaging structure with dynamic binding rather than function calling.
- Memory for objects is always allocated in heap space and never on the stack.
- Some variables like CGRect not holding Objective-C objects are using stack space.
Item 2: Minimize Importing Headers in Headers
- Use forward declaration to decrease compile time. EX: @class EOCEmployer;
- 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];
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.
Similar to above.
If a mutable variant is required, a mutable copy must be taken.
EX: NSMutableArray *mutable = [@[@1, @2, @3, @4, @5] mutableCopy];
沒有留言:
張貼留言