トップ «前の日記(2012-01-24) 最新 次の日記(2012-01-27)» 編集

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|

2012-01-25 [Mac][iOS]無名カテゴリ

Objective-Cで、非公開メソッドが必要になった、無名カテゴリで実現できる。

@interface MyClass : NSObject {
}
- (void)publicMethod;
@end
 
@interface MyClass ()
- (void)privateMethod;
@end

カテゴリには、メソッドしか宣言できない為、無名カテゴリで非公開に出来るのはメソッドのみと考えていたが、Clang/LLVM 2.0コンパイラを使うとプロパティでもOKのようだ。

@interface MyClass : NSObject
- (void)publicMethod;
@end
 
@interface MyClass ()
@property (nonatomic, strong) NSMutableString   *string;
- (void)privateMethod;
@end
 
@implementation MyClass
@synthesize string = _string;
@end

通常のクラス宣言部にも同じプロパティの宣言をして、公開用と非公開用で宣言の内容を変える事も可能なようだ。

@interface MyClass : NSObject
@property (nonatomic, strong, readonly) NSMutableString   *string;
- (void)publicMethod;
@end
 
@interface MyClass ()
@property (nonatomic, strong, readwrite) NSMutableString   *string;
- (void)privateMethod;
@end
 
@implementation MyClass
@synthesize string = _string;
@end

無名カテゴリは、AppleのDeveloper Libraryの説明によると、匿名のカテゴリに似ているが、正確にはクラス拡張と呼ぶ、ちょっと、異なる物のようだ。

_ 関連情報

Categories and Extensions
iOS Developer LibraryのThe Objective-C Programming Languageでの無名カテゴリの説明です。

トップ «前の日記(2012-01-24) 最新 次の日記(2012-01-27)» 編集