Sean's Note: 2月 2015

2015年2月17日 星期二

initWithNibName 和 loadNibNamed 方法的區別

initWithNibNameloadNibNamed 都可以用來建立 Custom ViewController,
// 延遲載入,用到時才會值
MyViewController *viewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];

MyViewController *viewController = [[[NSBundle mainBundle] loadNibNamed:@"MyViewController" owner:self options:nil] objectAtIndex:0];

但是在 NIB 上的設定會有不同:
initWithNibName 的 XIB 需要在 Placeholders 設置 File's Owner 的 Custom Class,
View Controller 則不須設置 Custom Class。而且拉的元件是 UIView。
反之,用 loadNibNamed 的 XIB 不能在 Placeholders 設置 File's Owner 的 Custom
Class,而 View Controller 則須設置 Custom Class。而且拉的元件是 UIViewController。
若兩者誤用,就會跑出 setValue:forUndefinedKey 的錯誤。

2015年2月11日 星期三

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

4. Protocols and Categories

Item 23: Use Delegate and Data Source Protocols for Interobject Communication 

  1. A delegate property will always be defined using the weak attribute.
  2. If required, implement the bitfield structure approach to cache which methods a delegate responds to from the protocol. 

Item 24: Use Categories to Break Class Implementation into Manageable Segments

  1. Use categories to split a class implementation into more manageable fragments.
  2. Create a category called Private to hide implementation detail of methods that should be considered as private.

Item 25: Always Prefix Category Names on Third-Party Classes

Item 26: Avoid Properties in Categories

  1. Can use associated objects instead, but not good enough.

Item 27: Use the Class-Continuation Category to Hide Implementation Detail

Item 28: Use a Protocol to Provide Anonymous Objects 

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.

Core Data framework

Managed Objects and Contexts

可以把 managed objects contexts 想像成是智慧型的便條紙,
當從 persistent store 獲取資料時,這些資料的副本就會記錄在便條紙上,
並形成 object graph,這些副本可以被修改,直到儲存後 persistent store
也才會真的改變。
Model objects(詳見 MVC Pattern) 在 Core Data framework 中即是
managed objects,所有的 managed objects 都須向 managed object context
註冊,不管新增或移除 objects 都是透過 context。
每個 Application 可以有多個 managed object context 。

參考: http://www.cocoanetics.com/2012/07/multi-context-coredata/

Fetch Requests

可以建立 fetch requests(NSFetchRequest) 向 managed object context 擷取資料,
由於所有的 managed objects 都須向 managed object context 註冊,所以這些回
傳的 objects 將自動與請求的 context 註冊。

Persistent Store Coordinator

介於 managed objects contexts 和 persistent object stores 的是 persistent store
coordinator(圖1),

圖1. (來源: iOS Developer Library)