简体   繁体   English

关于iOS6 +中的@synthesize

[英]Regarding @synthesize in iOS6+

Eg: 例如:

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

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

So I know that we don't need to use @synthesize any more from iOS6+. 所以我知道我们不再需要在iOS6 +中使用@synthesize The compiler automatically creates getters and setters for me. 编译器会自动为我创建getter和setter。

  1. But I don't understand is when to use self.name = @"Testing" and when to use _name = @"Testing" ? 但是我不明白是什么时候使用self.name = @"Testing"以及什么时候使用_name = @"Testing"

  2. Should _name = @"Testing" be ever used? 应该使用_name = @"Testing"吗?

  3. If yes, when? 如果是,什么时候? When should an iVAR be used at all? 什么时候应该使用iVAR?

  4. Also if i want to write my own getter and setter do i need to write @synthesize or can i just write my getter and setter? 另外,如果我想编写自己的getter和setter,我是否需要写@synthesize还是可以只编写我的getter和setter?

Thanks for your help! 谢谢你的帮助!

Normally, you only use instance variables in init methods, getters and setters, and dealloc. 通常,只在init方法,getter和setter以及dealloc中使用实例变量。 There are exceptions of course, but this is a good rule of thumb. 当然也有例外,但这是一个很好的经验法则。

If you write both your own getter and setter (or in the case of a readonly property and you write the getter), you have to synthesize your property yourself. 如果您既编写自己的getter setter(或者在编写只读属性的情况下编写getter),则必须自己合成属性。 In all other cases, the property is auto-synthesized. 在所有其他情况下,该属性是自动合成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM