问题描述
我正在使用iOS5书来学习iOS编程。
I am using an iOS5 book to learn iOS programming.
@synthesize coolWord;
^ synthesize用于.m文件中的所有属性
^synthesize is used for all properties in .m files
我听说在iOS6中不需要合成,因为它是自动完成的。这是真的吗?
I heard that in iOS6 there is no need for synthesize, since it is automatically done for you. Is this true?
合成是否会为iOS6扮演任何角色?
Does synthesize play any role for iOS6?
感谢您的澄清。 :)
推荐答案
在objective-c中的@synthesize只实现属性设置器和getter:
@synthesize in objective-c just implements property setters and getters:
- (void)setCoolWord:(NSString *)coolWord {
_coolWord = coolWord;
}
- (NSString *)coolWord {
return _coolWord;
}
Xcode 4的确适用于此(iOS6需要Xcode) 4)。从技术上讲,它实现了 @synthesize coolWord = _coolWord
( _coolWord
是实例变量而 coolWord
是属性)。
It is true with Xcode 4 that this is implemented for you (iOS6 requires Xcode 4). Technically it implements @synthesize coolWord = _coolWord
(_coolWord
is the instance variable and coolWord
is the property).
要访问这些属性,请使用 self.coolWord
进行设置 self.coolWord = @YEAH!;
并获得 NSLog(@%@,self.coolWord);
To access these properties use self.coolWord
both for setting self.coolWord = @"YEAH!";
and getting NSLog(@"%@", self.coolWord);
另请注意,setter和getter仍然可以手动实现。如果你同时实现了setter和getter,你还需要手动包含 @synthesize coolWord = _coolWord;
(不知道为什么会这样)。
Also note, both the setter and getter can still be manually implemented. If you implement BOTH the setter and getter though you NEED to also manually include @synthesize coolWord = _coolWord;
(no idea why this is).
这篇关于合成的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!