2011-10-12 64 views
5

我注意到Objective-c類的各種@interface聲明。我想知道爲什麼開發商申報@interface在以下幾個方面:各種@interface聲明,有些用括號

// in the .h file 
@interface MyClass : NSObject 
// ... 
@end 

// in the .m file (what's the purpose of the parens?) 
@interface MyClass() 
// more property declarations which seem like they can go in the .h file 
@end 

// again in the .m file (what's the purpose of private?) 
@interface MyClass (Private) 
// some method declarations 
@end 
+1

例如:[快速搜索(http://stackoverflow.com/search?q=%5Bobjc%5D+%40interface+parentheses&submit=search)變成了HTTP ://stackoverflow.com/questions/7378479/what-does-the-text-inside-parentheses-in-interface-and-implementation-directive –

+1

看看[language spec]怎麼樣(http://developer.apple .COM /庫/ MAC /文檔/可可/概念/的ObjectiveC /章節/ ocDefiningClasses.html#// apple_ref/DOC/UID/TP30001163-CH12-SW1)? –

回答

5

這只是一個正常class interface,從NSObject的,在這裏聲明實例變量,屬性和方法

// in the .h file 
@interface MyClass : NSObject 
// ... 
@end 

以下兩個是categories,它允許你的方法添加到一個類繼承。但它不是一個子類(不要聲明一個名稱相同的方法,因爲你將無法訪問原來的方法)。如果您有一個接口的命名類別(如@interface MyClass (Private)),則應在​​中提供實現,對於未命名的類別(也稱爲擴展),可以像平常一樣提供實現。請注意,擴展還允許您將ivars添加到類中,而(命名)類別則不會。

// in the .m file (what's the purpose of the parens?) 
@interface MyClass() 
// more property declarations which seem like they can go in the .h file 
@end 

// again in the .m file (what's the purpose of private?) 
@interface MyClass (Private) 
// some method declarations 
@end 
+1

目前有可能將存儲添加到擴展中的類中。通過一個類別不可能做到這一點。 –

-1

曾雲在.m文件什麼是私人的。 parens是用於類別的,所以你可以將你的代碼分成不同的類別以使其更具可讀性。因爲代碼是在.m和私人,他們稱爲類別私人。

相關問題