使用@property和@synthesise?

aku*_*tsu 3 properties declaration objective-c ios

我想知道@property和@synthesise有什么意义.目前我使用以下内容来声明一些内容:

//Class.m
#import "Class.h"

CCNode *node;

@implementation
//init, etc..
Run Code Online (Sandbox Code Playgroud)

但我见过别人用过:

@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.
Run Code Online (Sandbox Code Playgroud)

它们似乎都以同样的方式工作,@ property和@synthesise方式的优点是什么?他们做不同的事情,如果是的话,那是什么?

ris*_*shi 5

@property - 创建getter和setter的声明.

@synthesize - 根据属性内传递的参数提供getter和setter的定义.

检查了这一点,也有差不多目前很多详细信息那里- https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html


ale*_*oot 5

@property并且@synthesize是两个客观的C关键字,允许您轻松创建属性,因此避免手动编写属性的getter和setter方法.

@property定义属性本身,应放置在头文件,并可以得到一些属性(例如:强,非原子,保留分配,副本),则@synthesize应该放到实现文件,并告诉编译器生成的主体getter和setter方法.

这两个关键字在正确使用其属性时非常有用,因为它们负责生成属性代码,并且最重要的是它们负责属性的内存管理.