【发布时间】:2012-01-09 22:38:39
【问题描述】:
我对这两个代码段感到困惑:
第一:
//.h
@interface Student : NSObject {
}
@property (nonautomic, copy) NSString *name;
@property (nonautomic, retain) NSNumber *age;
@end
//.m
@implementation Student
@synthesize name;
@synthesize age;
@end
第二:
//.h
@interface Student : NSObject {
NSString *name; // <<============ difference
NSNumber *age; // <<============ difference
}
@property (nonautomic, copy) NSString *name;
@property (nonautomic, retain) NSNumber *age;
@end
//.m
@implementation Student
@synthesize name;
@synthesize age;
@end
这两种方法都可以。那么有必要在{}中声明变量吗?
【问题讨论】:
-
您的属性应标记为
nonatomic,而不是nonaUtomic -
@5StringRyan 这个问题需要你了解运行时有多个版本,这来自不同的知识水平。
-
@JoshuaWeinberg - 我不明白,他问“那么是否需要在 {} 中声明变量”,我指出的 SO 帖子的创建者说,“这仍然有效吗? ?” (指创建没有 ivars 的属性)。此外,该帖子中接受的答案与您的答案非常相似。
-
哦,我理解并同意答案是相同的,但问题却大不相同:)
标签: objective-c interface properties instance-variables