Last Update 2013/02/13
TOP - Objective-C - @public
インスタンス変数の有効範囲を設定しません。
どこからでもアクセス可能となります。
どこからでもアクセス可能となります。
@public リスト1
リスト1
インスタンス変数宣言リスト
次のコンパイラ指定が出現するまで有効
次のコンパイラ指定が出現するまで有効
(例)
※ Xcodeで生成される「***_Prefix.pch」などのファイルの内容は記載しません。
#import <Foundation/Foundation.h>
@interface MyTestClass1 : NSObject
{
@public
NSString *messageString1;
}
- (void)testMethod;
@end
@implementation MyTestClass1
- (void)testMethod
{
messageString1 = [NSString stringWithFormat:@"MyTestClass1"];
NSLog(@"%@", messageString1);
}
@end
@interface MyTestClass2 : NSObject
{
NSString *messageString1;
}
- (void)testMethod;
@end
@implementation MyTestClass2
- (void)testMethod
{
messageString1 = [NSString stringWithFormat:@"MyTestClass2"];
NSLog(@"%@", messageString1);
}
@end
int main (void)
{
MyTestClass1 *c1 = [[MyTestClass1 alloc] init];
MyTestClass2 *c2 = [[MyTestClass2 alloc] init];
[c1 testMethod];
[c2 testMethod];
NSLog(@"c1 : %@", c1->messageString1);
NSLog(@"c2 : %@", c2->messageString1);
// 実行されるものの警告発生(this will be a hard error in the future)
return 0;
}
実行結果
MyTestClass1
MyTestClass2
c1 : MyTestClass1
c2 : MyTestClass2