在什么情况下,Objective-c中的@synthesize是自动的? [英] Under what conditions is @synthesize automatic in Objective-c?
问题描述
在什么情况下,Objective-c中的@synthesize自动生成?
也许在使用LLVM 3.0及更高版本时?从网络上看,从Xcode 4开始似乎不需要@synthesize
.但是,我使用Xcode 4并在没有@synthesize
属性时收到警告.
Perhaps when using LLVM 3.0 and up? From reading around the net it seems like @synthesize
is unnecessary starting with Xcode 4. However I'm using Xcode 4 and receiving warnings when I don't @synthesize
a property.
对为什么不自动合成属性的一些回应似乎暗示@synthesize在某些情况下可以在某些时候省略.
Some of the responses to Why don't properties get automatically synthesized seem to imply @synthesize can be omitted at some point under some circumstances.
另一(旧)参考提示@synthesize
在将来的某个时候可能会自动出现.
Another (old) reference hinting that @synthesize
might be automatic at some point in the future.
推荐答案
从clang 3.2(大约在2012年2月)开始,默认情况下提供了Objective-C属性的默认综合"(或自动属性综合").基本上就像您最初阅读的博客文章所述: http: //www.mcubedsw.com/blog/index.php/site/comments/new_objective-c_features/(除了该帖子将功能描述为启用,然后禁用";我不知道这是否是Xcode的问题,或者clang开发人员本身是否来回讨论此问题.
As of clang 3.2 (circa February 2012), "default synthesis" (or "auto property synthesis") of Objective-C properties is provided by default. It's essentially as described in the blog post you originally read: http://www.mcubedsw.com/blog/index.php/site/comments/new_objective-c_features/ (except that that post describes the feature as "enabled, then disabled"; I don't know if that's an issue with Xcode or if the clang developers themselves have gone back and forth on the question).
据我所知,在clang 3.2中默认不合成属性的唯一情况是这些属性是从协议继承的.这是一个示例:
As far as I know, the only case in which properties will not be default-synthesized in clang 3.2 is when those properties have been inherited from a protocol. Here's an example:
#import <Foundation/Foundation.h>
@protocol P
@property int finicky;
@end
@interface A : NSObject <P>
@property int easygoing;
@end
@implementation A
@end
int main() { A *a = [A new]; a.easygoing = 0; a.finicky = 1; }
如果您编译此示例,则会收到警告:
If you compile this example, you'll get a warning:
test.m:11:17: warning: auto property synthesis will not synthesize property
declared in a protocol [-Wobjc-protocol-property-synthesis]
@implementation A
^
test.m:4:15: note: property declared here
@property int finicky;
^
1 warning generated.
如果您运行它,则会在运行时收到错误消息:
and if you run it, you'll get an error from the runtime:
objc[45820]: A: Does not recognize selector forward:: (while forwarding setFinicky:)
Illegal instruction: 4
这篇关于在什么情况下,Objective-c中的@synthesize是自动的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!