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

2015年2月10日 星期二

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

3. Interface and API Design

Item 15: Use Prefix Names to Avoid Namespace Clashes

  1. Choose a class prefix that befits your company, application, or both. Then stick with that prefix throughout.
  2. If you use a third-party library as a dependency of your own library, consider prefixing its names with your prefix. 

Item 16: Have a Designated Initializer

  1. Throw an exception in initializers overridden from superclasses that should not be used in the subclass. 

Item 17: Implement the description Method

  1. -(NSString*)description,   -(NSString*)debugDescription

Item 18: Prefer Immutable Objects

Item 19: Use Clear and Consistent Naming

  1. Verbose but clear naming of methods is more acceptable in Objective-C than in other languages.
  2. Avoid abbreviations, such as str, and instead use full names, such as string.

Item 20: Prefix Private Method Names

  1. Suggestion: Use "p_" as prefix for private methods.
  2. Avoid using single "_" as the method prefix, since this is reserved by Apple.

Item 21: Understand the Objective-C Error Model

  1. Any objects that should be released at the end of a scope in which an exception is thrown will not be released. (-fobjc-arc-exceptions)

Item 22: Understand the NSCopying Protocol

  1. To support copying: 
    1. Implement NSCopying protocol. 
    2. Override -(id)copyWithZone:(NSZone*)zone or -(id)mutableCopyWithZone:(NSZone*)zone 
  2. If your object has mutable and immutable variants, implement both the NSCopying and NSMutableCopying protocols.

沒有留言:

張貼留言