我想知道@property 和@synthesese 的意义何在。目前我使用以下声明:
//Class.m
#import "Class.h"
CCNode *node;
@implementation
//init, etc..
但我看到其他人使用:
@property (nonatomic, etc..) CCNode* node;
@synthesise (nonatomic, etc..) node;
//I am not too sure on how this type of declaration works, please correct me on how it's done.
它们的工作方式似乎相同,@property 和@synthesese 方式的优点是什么?他们做不同的事情吗?如果是,那是什么?
最佳答案
@property
和 @synthesize
是两个 Objective-C 关键字,可让您轻松创建属性,从而避免手动编写属性的 getter 和 setter 方法。
@property
定义属性本身,应该放在头文件中并且可以获取一些属性(例如:strong,nonatomic,retain assign,copy),@ synthesize
应该放在实现文件中,告诉编译器生成getter和setter方法体。
这两个关键字在正确使用它们的属性时非常有用,因为它们负责属性代码的生成,最重要的是它们负责内存属性(property)管理。
关于objective-c - 使用@property 和@synthesese?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10799015/