关于iOS6 +中的@synthesize

时间:2013-10-13 04:42:34

标签: ios objective-c

例如:

//Myclass.h
@property(nonatomic, strong) NSString *name;

//MyClass.m
//@synthesize name = _name

所以我知道我们不需要再使用iOS6 +中的@synthesize了。编译器会自动为我创建getter和setter。

  1. 但我不明白何时使用self.name = @"Testing"以及何时使用_name = @"Testing"

  2. 是否应该使用_name = @"Testing"

  3. 如果是,何时?什么时候应该使用iVAR?

  4. 另外如果我想写自己的getter和setter,我需要写@synthesize还是我可以写我的getter和setter?

  5. 感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

通常,您只在init方法,getter和setter以及dealloc中使用实例变量。当然也有例外,但这是一个很好的经验法则。

如果您同时编写自己的getter setter(或者在readonly属性的情况下编写getter),则必须自己合成属性。在所有其他情况下,该属性是自动合成的。

相关问题