Features of use @property and @synthesize (cocos2d)

喜欢而已 提交于 2019-12-23 06:13:24

问题


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

Standard in the examples is written as follows:

in .h

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

in .m

@synthesize minimumTouchLengthToSlide

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

in .h

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

in .m

@synthesize minimumTouchLengthToSlide = minimumTouchLengthToSlide_;

What is the meaning of this code?

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


回答1:


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;

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_;

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).




回答2:


@property and @synthesize are a really cool feature of Objective-C to allow the automatic creation of getter and setter methods. In your examples they would create:

- (CGFloat)minimumTouchLengthToSlide and

- (void)setMinimumTouchLengthToSlide:(CGFloat)newLength; for free.

@synthesize minimumTouchLengthToSlide = minimumTouchLengthToSlide_ means they are telling Objective-C that when someone tries to access that property, then it should point at the instance variable minimumTouchLengthToSlide_

readwrite,assign describe what happens when someone sets the property. Assign means that the value is not retained, the variable is just pointed. An example of what that method might look like could be this:

- (void)setMinimumLengthToSlide:(CGFloat)newLength {
    [self willChangeValueForKey:@"minimumLengthToSlide"]; // let observers know this property is changing
    minimumLengthToSlide_ = newLength;

    [self didChangeValueForKey:@"minimumLenghtToSlide"];
}

You can read more about them here.



来源:https://stackoverflow.com/questions/8145751/features-of-use-property-and-synthesize-cocos2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!