2013年9月13日 星期五

為何objective c 要在.m檔中再@interface ViewControllor ()一次?是category嗎?

在XCode建立的UIViewController中,我們會在.m檔中看到
@interface ViewControllor ()
@end

在其中的宣告的內容,效果跟在.h中一樣,但是不會在.h中被其他人看到。
所以我們可以用來
  1. 修改.h中對property的設定,例如對外是宣告為readonly,在這再宣告回readwrite。
  2. 宣告primary method。

接下來是說明category,其實上面的class extention做法就可以看成是category的一個特例
下面可以看出其中的差別:在category中括號內部要放入你要作用的Class Name(上面提的沒有)
@interface NSObject(MySuperObject)
-(void) printRetainCount;
+(void) sayHello;
@end
 
category是讓你針對你繼承的class來新增你要的method,
或者是改寫其中某一個原來就有的method。 

沒有留言:

張貼留言