2011-10-12 7 views
5

Objective-cクラスのさまざまな宣言に気付きました。私は、開発者が@interfaceを宣言する理由を次の方法で理解したいと思います。これまで.mファイルに行く何@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

[言語仕様](http://developer.apple.com)を見てどうですか? .com/library/mac/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocDefiningClasses.html#// apple_ref/doc/uid/TP30001163-CH12-SW1)? –

答えて

5

これは、あなたがクラスにメソッドを追加できるようにする2つですcategories次アイバーズ、プロパティとメソッド

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

を宣言NSObjectの、継承、普通class interfaceです。ただし、サブクラスではありません(元のメソッドにアクセスできないため、同じ名前のメソッドを宣言しないでください)。インターフェイスの名前付きカテゴリ(@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とプライベートであるため、プライベートカテゴリと呼ばれます。

関連する問題