使用@property和@synthesize的特性(cocos2d) [英] Features of use @property and @synthesize (cocos2d)

查看:238
本文介绍了使用@property和@synthesize的特性(cocos2d)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在库中看到使用cocos2d strange @property和@synthesize

I saw in the libraries for use cocos2d strange @property and @synthesize

标准在例子中写如下:

in .h

CGFloat minimumTouchLengthToSlide; 
}
@property(readwrite, assign) CGFloat minimumTouchLengthToSlide;

in .m

@synthesize minimumTouchLengthToSlide

但是在lib https://github.com/cocos2d/cocos2d-iphone-extensions/tree/master/Extensions/CCScrollLayer 和另一个libs\extensions

But in lib https://github.com/cocos2d/cocos2d-iphone-extensions/tree/master/Extensions/CCScrollLayer and another libs\extensions

.h

CGFloat minimumTouchLengthToSlide_; 
}
@property(readwrite, assign) CGFloat minimumTouchLengthToSlide;

in .m

@synthesize minimumTouchLengthToSlide = minimumTouchLengthToSlide_;

此代码的含义是什么?

What is the meaning of this code?

为什么他们将minimumTouchLengthToSlide更改为minimumTouchLengthToSlide_并添加了minimumTouchLengthToSlide = minimumTouchLengthToSlide _;

Why they changed minimumTouchLengthToSlide to minimumTouchLengthToSlide_ and added minimumTouchLengthToSlide = minimumTouchLengthToSlide_;

推荐答案

p>它通常被认为是命名实例变量不同于属性的好习惯。这背后的共鸣是,在这种情况下,你不能意外使用实例变量而不是属性。当使用诸如整数和浮点数的值类型时,这并不重要,但在 retain 属性上使用引用类型时更重要。考虑一个属性

Its often considered good practice to name the instance variable different from the property. The resoning behind this is that in that case you cannot accidently use the instance variable instead of the property. This is not that important when using value types such as integers and floats but more important when using reference types on retain properties. Consider a property

@property (nonatomic, retain) NSString *myString;
...
@synthesize myString;

编译器在执行 self.myString = someString 。但是当你写 myString = someString 时,你实际上不使用属性,而是直接使用变量,不会发生保留。这可以导致僵尸,泄漏等。通过给实例变量一个不同的名称,像这样:

The compiler takes care of retaining the string when you do self.myString = someString. But when you write myString = someString you do not actually use the property but rather the variable directly and no retaining will take place. This can lead to zombies, leaks etc. By giving the instance variable a different name like this:

@property (nonatomic, retain) NSString *myString;
...
@synthesize myString = myString_;

您不能再写 myString = someString 因为这将发出编译器错误。如果你需要直接使用实例变量,你总是可以写 _myString = someString ,但实际上很少需要。

you can no longer write myString = someString because this would issue a compiler error. If you needed to use the instance variable directly you could always write _myString = someString but in practice this is rarely needed.

还有其他情况下,当你写明确的属性方法,但问题基本上是一样的,你不能不小心绕过属性方法,当使用第二个变量。

There are other cases when you write explicit property methods but the issue is basically the same, you cannot accidently bypass the property methods when using the second variant.

这是一种避免在处理属性(大部分是保留属性)时不必要的错误的方法。

So basically this is a method to avoid unnecessary errors when handling properties (mostly retain-properties).

这篇关于使用@property和@synthesize的特性(cocos2d)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆