【发布时间】:2010-09-06 05:31:59
【问题描述】:
@property 和 @synthesize 有什么用?可以举个例子解释一下吗?
【问题讨论】:
-
帮助自己,读一本书或苹果的Objective C介绍:developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/…
标签: objective-c
@property 和 @synthesize 有什么用?可以举个例子解释一下吗?
【问题讨论】:
标签: objective-c
非常简短的回答:他们为 ivars 创建访问器。
有some examples on wikipedia。看看那些。
【讨论】:
您可以将属性声明视为等同于声明两个访问器方法。因此
@property float value;
相当于:
- (float)value;
- (void)setValue:(float)newValue;
通过使用@synthesize,编译器会为您创建访问器方法(查看更多here)
【讨论】:
// Sample for @property and @sythesize //
@interface ClassA
NSString *str;
@end
@implementation ClassA
@end
The Main Function main()
//确保你#import ClassA
ClassA *obj=[[ClassA alloc]init];
obj.str=@"XYZ"; // That time it will give the error that we don't have the getter or setter method. To use string like this we use @property and @sythesize
【讨论】: