2011-09-11 35 views
22

我有一個關於Apple的一些示例代碼的非常基本的問題。在.m文件中,類聲明如下所示:@interface和@implementation指令中圓括號內的文字是什麼意思?

@interface MyMovieViewController (OverlayView) 
[...] 
@end 

@interface MyMovieViewController (ViewController) 
[...] 
@end 

@implementation MyMovieViewController(ViewController) 
[...] 
@end 

@implementation MyMovieViewController (OverlayView) 
[...] 
@end 

@implementation MyMovieViewController 
[...] 
@end 

完整代碼here

看起來像括號內的東西(「OverlayView」和「ViewController」)只是幫助分解代碼並使其更具可讀性,但實際上並沒有影響代碼的執行。但我不想誤解重要的事情,所以我想我會檢查確定。

我的理解正確嗎?謝謝!

回答

20

那些被稱爲Categories,讓您進一步的功能添加到您的類。

一個類別允許您將方法添加到現有的類 - 即使是沒有源的類也是如此。類別是一個強大的功能,它允許您在不使用子類的情況下擴展現有類的功能。使用類別,您還可以在多個文件中分發您自己的類的實現。類擴展類似,但允許在除了主類@interface塊之外的位置爲類聲明額外的必需API。

From the Apple docs on Categories and Extensions.

上[定製現有類]
+0

蘋果文檔(https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html) – LopSae

相關問題