![](/img/trans.png)
[英]Is there a difference between an "instance variable" and a "property" in Objective-c?
[英]Difference between private instance variable and property in class extension (Objective-c 2.0)
以下Objective-c 2.0代碼段之間有什么區別(如果有):
// in MyClass.h
@interface MyClass
@private
NSString *myString;
@end
和
// in MyClass.m
@interface MyClass ()
@property (nonatomic, copy) NSString *myString;
@end
@implementation MyClass
@synthesize myString;
@end
ivar(第一個)是一個普通變量,無法在創建它的接口的實現的范圍之外進行訪問(如果使用@private指令),並且沒有合成的訪問器方法。
該屬性(第二個屬性)是一個包裝的ivar,它總是可以通過實例化一個類來訪問,並且具有綜合的訪問器方法(如果正在使用@synthesize指令)
MyClass *class = [[MyClass alloc] init];
[class setMyString:@"someString"]; //generated setter
NSString *classString = [class myString]; //generated getter
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.