最近 Xcode 4.3.2 に更新したところ、次@implementation
のようにブロック内でプライベート インスタンス変数を宣言できるようになりました。
@interface TestClass : NSObject
@property (nonatomic, copy) NSString *testProp;
@end
@implementation TestClass {
NSString *_testPropStore;
}
- (NSString *)testProp { return _testPropStore; }
- (void)setTestProp:(NSString *)testProp { _testPropStore = [testProp copy]; }
- (id)init {
if (self = [super init]) {
_testPropStore = nil;
}
return self;
}
@end
ブレース ブロックNSString *_testPropStore
内の行に注目してください。@implementation
次のコードでもテストしました。
TestClass *c1 = [[TestClass alloc] init];
TestClass *c2 = [[TestClass alloc] init];
c1.testProp = @"Hello";
c2.testProp = @"World";
NSAssert(c1.testProp == c2.testProp, @"It's working!");
これはうまくいくようです。(つまり、アプリは NSAssert 行で「動作しています」というメッセージでクラッシュします。)
これは、プライベート インスタンス変数を宣言するための Objective-C の新機能ですか? 私は偶然これを発見したので、プライベートインスタンス変数を宣言するためだけのものなのか、それとも私が気付いていない副作用があるのか知りたいです?
そのようなタイプの単語を含むほとんどの質問は、private
異なるプライベート拡張カテゴリでそれらを宣言する方法についての回答になってしまったため、関連するドキュメントを見つけることができませんでした。