#import <Foundation/Foundation.h>
@interface Visibility : NSObject {
@public
BOOL boolPublic;
@protected
BOOL boolProtected;
@private
BOOL boolPrivate;
}
@property (nonatomic, assign) BOOL boolPublic;
@property (nonatomic, assign) BOOL boolProtected;
@property (nonatomic, assign) BOOL boolPrivate;
@end
@implementation Visibility
@synthesize boolPublic;
@synthesize boolProtected;
@synthesize boolPrivate;
@end
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Visibility *visibility = [[Visibility alloc] init];
visibility.boolPublic = YES;
visibility.boolProtected = YES;
visibility.boolPrivate = YES;
// Place following NSLog()'s here
[pool release];
}
讓我們試試這個
使用您定義的方法@財產/ @合成
NSLog(@"Accessors %d %d %d", visibility.boolPublic, visibility.boolProtected, visibility.boolPrivate);
=> 2012-01-08 17:46:40.226 Untitled[2592:707] Accessors 1 1 1
訪問@公共伊娃直接
NSLog(@"Public %d", visibility->boolPublic);
=> 2012-01-08 17:46:40.228 Untitled[2592:707] Public 1
訪問@protected伊娃直接
NSLog(@"Protected %d", visibility->boolProtected);
=> error: instance variable 'boolProtected' is protected
=> NSLog(@"Protected %d", visibility->boolProtected);
=> ^
訪問@private伊娃直接
NSLog(@"Private %d", visibility->boolPrivate);
=> error: instance variable 'boolPrivate' is private
=> NSLog(@"Private %d", visibility->boolPrivate);
=> ^
當您使用點表示法訪問此:
visibility.boolPublic
等同於:
[visibility boolPublic]; // <- This is a method call
而@package制約可視性的框架。 – Abizern 2012-01-08 17:43:38