ios 合成目的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14658142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Purpose of Synthesize
提问by JoseSwagKid
I am using an iOS5 book to learn iOS programming.
我正在使用一本 iOS5 书来学习 iOS 编程。
@synthesize coolWord;
^synthesize is used for all properties in .m files
^synthesize 用于 .m 文件中的所有属性
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?
合成对 iOS6 有什么作用吗?
Thanks for the clarification. :)
感谢您的澄清。:)
回答by Firo
@synthesize in objective-c just implements property setters and getters:
Objective-c 中的 @synthesize 只是实现了属性设置器和获取器:
- (void)setCoolWord:(NSString *)coolWord {
_coolWord = coolWord;
}
- (NSString *)coolWord {
return _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).
Xcode 4 确实为您实现了这一点(iOS6 需要 Xcode 4)。从技术上讲,它实现@synthesize coolWord = _coolWord
(_coolWord
是实例变量,coolWord
是属性)。
To access these properties use self.coolWord
both for setting self.coolWord = @"YEAH!";
and getting NSLog(@"%@", self.coolWord);
要访问这些属性,请self.coolWord
同时使用设置self.coolWord = @"YEAH!";
和获取NSLog(@"%@", self.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).
另请注意,setter 和 getter 仍然可以手动实现。如果您同时实现了 setter 和 getter,尽管您还需要手动包含@synthesize coolWord = _coolWord;
(不知道为什么会这样)。
回答by Jano
Autosynthesis in iOS6 still requires @synthesize
iOS6 中的自动合成仍然需要 @synthesize
- to generate accessor methods for properties defined in a
@protocol
. - to generate a backing variable when you included your own accessors.
- 为 中定义的属性生成访问器方法
@protocol
。 - 在包含自己的访问器时生成支持变量。
The second case can be verified like this:
第二种情况可以这样验证:
#import <Foundation/Foundation.h>
@interface User : NSObject
@property (nonatomic, assign) NSInteger edad;
@end
@implementation User
@end
Type: clang -rewrite-objc main.m
and check that the variable is generated. Now add accessors:
键入:clang -rewrite-objc main.m
并检查变量是否已生成。现在添加访问器:
@implementation User
-(void)setEdad:(NSInteger)nuevaEdad {}
-(NSInteger)edad { return 0;}
@end
Type: clang -rewrite-objc main.m
and check that the variable is NOT generated. So in order to use the backing variable from the accessors, you need to include the @synthesize
.
键入:clang -rewrite-objc main.m
并检查变量是否未生成。因此,为了使用访问器的后备变量,您需要包含@synthesize
.
It may be related to this:
可能与此有关:
Clang provides support for autosynthesis of declared properties. Using this feature, clang provides default synthesis of those properties not declared @dynamic and not having user provided backing getter and setter methods.
Clang 为声明的属性的自动合成提供支持。使用此功能,clang 提供了那些未声明 @dynamic 并且没有用户提供支持 getter 和 setter 方法的属性的默认合成。
回答by barndog
I'm not sure how @synthesize
relates to iOS6 but since Xcode 4.0, it's essentially been deprecated. Basically, you don't need it! Just use the @property
declaration and behind the scenes, the compiler generates it for you.
我不确定@synthesize
与 iOS6 的关系,但从 Xcode 4.0 开始,它基本上已被弃用。基本上,你不需要它!只需使用@property
声明并在幕后,编译器为您生成它。
Here's an example:
下面是一个例子:
@property (strong, nonatomic) NSString *name;
/*Code generated in background, doesn't actually appear in your application*/
@synthesize name = _name;
- (NSString*)name
{
return _name;
}
- (void) setName:(NSString*)name
{
_name = name;
}
All that code is taken care of the complier for you. So if you have an applications that have @synthesize
, it's time to do some cleanup.
所有这些代码都由编译器为您处理。因此,如果您的应用程序具有@synthesize
,那么是时候进行一些清理工作了。
You can view my similar question herewhich might help to clarify.
回答by aakash
I believe that @synthesize
directives are automatically inserted in the latest Obj-C compiler (the one that comes with iOS 6).
我相信@synthesize
指令会自动插入到最新的 Obj-C 编译器(iOS 6 附带的编译器)中。
The point of @synthesize pre-iOS 6 is to automatically create getters & setters for instance variables so that [classInstance getCoolWord]
and [classInstance setCoolWord:(NSString *)aCoolWord]
are generated. Because they are declared with @property
, you also get the convenience of dot syntax for the getter and setter.
@synthesize点预-IOS 6是自动创建的实例变量的getter&设置器以便[classInstance getCoolWord]
和[classInstance setCoolWord:(NSString *)aCoolWord]
被生成。因为它们是用 声明的@property
,所以您还可以获得 getter 和 setter 的点语法的便利。
回答by Anurag Bhakuni
hope this will help little more
yes previously we have to synthesis the property by using @synthesis now it done by IDE itself.
but we can use it like
但我们可以像这样使用它
// what IDE does internally
// IDE 在内部做什么
@synthesis name=_name;
we use _name to access particular property but now you want synthesis by some other way like firstname you can do it like
我们使用 _name 来访问特定的属性,但现在你想要通过其他方式合成,比如 firstname 你可以这样做
@synthesis name= firstname
or just by name
或者只是名字
@synthesis name=name
回答by Dhruv Goel
With automatic synthesis in iOS6, it is no longer necessary to specifically declare backing ivars or write the @synthesize statement. When the compiler finds a @property statement, it will do both on our behalf using the guidelines we've just reviewed. So all we need to do is declare a property like this:
有了iOS6中的自动合成,不再需要专门声明backing ivars或者写@synthesize语句。当编译器找到@property 语句时,它将使用我们刚刚查看的指南代表我们执行这两项操作。所以我们需要做的就是声明一个这样的属性:
@property (nonatomic, strong) NSString *abc;
and in iOS 6, @synthesize abc = _abc, will be added automatically at compile time.
而在 iOS 6 中,@synthesize abc = _abc,将在编译时自动添加。