自动@property合成不适用于NSManagedObject子类 [英] Automatic @property synthesize not working on NSManagedObject subclass
问题描述
昨晚更新到适用于iOS6的最新版Xcode 4.5后,我收到类似的警告和错误
After updating to the newest Version of Xcode 4.5 for iOS6 last night, i get Warnings and Errors like this
Property'mapAnnotation'要求方法'mapAnnotation'定义
- 使用@synthesize,@ dynamic或在此类实现中提供方法实现
Property 'mapAnnotation' requires method 'mapAnnotation' to be defined - use @synthesize, @dynamic or provide a method implementation in this class implementation
因为缺少@synthesize语句,甚至是关于未知iVars的错误,如果我使用它们。
because of missing @synthesize Statements, and even Errors about unknown iVars if i use them.
问题是,我认为自从Mountain Lion发布的上一个Xcode Update到4.5以来,没有必要编写这些@synthesize语句,并且我的所有项目都工作了没有他们之前我昨晚更新了Xcode(我已经从我的文件中删除了一大堆@synthesize语句)
它甚至还在Release-Notes中:
The thing is, i thought it was not necessary to write these @synthesize statements since the last Xcode Update to 4.5 that came out with Mountain Lion, AND all my projects worked without them before i've updated Xcode last night (i've deleted a whole bunch of @synthesize statements from my files back then) It's even still in the Release-Notes:
•使用属性时默认生成Objective-C @synthesize命令。
• Objective-C @synthesize command is generated by default when using properties.
所以我很困惑,我错过了一个新的项目设置,它会自动生成@synthesize生成?
So i'm confused, am i missing a new Project-Setting that turns automatic @synthesize generation on?
但是当我创建一个新项目并尝试它时它甚至都没有工作
But it's not even working when i create a new Project and try it
推荐答案
我遇到了同样的问题并找到了原因和解决方案。
I faced the same problem and found the reason and the solution.
如果你看一下iOS 6 SDK中NSManagedObject的头文件,你会看到NS_REQUIRES_PROPERTY_DEFINITIONS强制类为属性指定@dynamic或@synthesize。
If you look at the header file of NSManagedObject in iOS 6 SDK, you'll see "NS_REQUIRES_PROPERTY_DEFINITIONS" which forces classes to specify @dynamic or @synthesize for properties.
(您可以在iOS 5.1和iOS 6.0之间的API差异中看到NS_REQUIRES_PROPERTY_DEFINITIONS。)
(You can see the NS_REQUIRES_PROPERTY_DEFINITIONS in the API diff between iOS 5.1 and iOS 6.0.)
这是因为编译器必须知道您是希望属性是动态的还是合成的,特别是在NSManagedObject类的子类的实现中。
This is because the compiler has to know if you want a property to be dynamic or synthesized especially in the implementation of a subclass of NSManagedObject class.
我可以简单地为NSManagedObject子类中除@dynamic之外的属性显式添加@synthesize行来解决这个问题。
I could solve this problem simply by adding the @synthesize lines explicitly for the properties other than @dynamic in NSManagedObject subclasses.
这篇关于自动@property合成不适用于NSManagedObject子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!