xcode 自动@property 合成不适用于 NSManagedObject 子类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12510849/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:32:21  来源:igfitidea点击:

Automatic @property synthesize not working on NSManagedObject subclass

xcodepropertiesios6synthesize

提问by MeXx

After updating to the newest Version of Xcode 4.5 for iOS6 last night, i get Warnings and Errors like this

昨晚更新到适用于 iOS6 的最新版本的 Xcode 4.5 后,我收到了这样的警告和错误

Property 'mapAnnotation' requires method 'mapAnnotation' to be defined - use @synthesize, @dynamic or provide a method implementation in this class implementation

属性 'mapAnnotation' 需要定义方法 'mapAnnotation' - 使用 @synthesize、@dynamic 或在此类实现中提供方法实现

because of missing @synthesize Statements, and even Errors about unknown iVars if i use them.

因为缺少@synthesize 语句,如果我使用它们,甚至会出现关于未知 iVar 的错误。

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:

问题是,我认为自从上次 Xcode 更新到 4.5 与 Mountain Lion 一起发布以来没有必要编写这些@synthesize 语句,并且在我昨晚更新 Xcode 之前,我所有的项目都在没有它们的情况下工作(我已删除当时我文件中的一大堆@synthesize 语句)它甚至仍在发行说明中:

? Objective-C @synthesize command is generated by default when using properties.

? 使用属性时默认生成 Objective-C @synthesize 命令。

So i'm confused, am i missing a new Project-Setting that turns automatic @synthesize generation on?

所以我很困惑,我是否错过了一个新的项目设置,它可以打开自动 @synthesize 生成?

But it's not even working when i create a new Project and try it

但是当我创建一个新项目并尝试它时它甚至不起作用

回答by Taka

I faced the same problem and found the reason and the solution.

我遇到了同样的问题,并找到了原因和解决方案。

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 6 SDK 中 NSManagedObject 的头文件,您会看到“NS_REQUIRES_PROPERTY_DEFINITIONS”,它强制类为属性指定 @dynamic 或 @synthesize。

(You can see the NS_REQUIRES_PROPERTY_DEFINITIONS in the API diff between iOS 5.1 and iOS 6.0.)

(您可以在 iOS 5.1 和 iOS 6.0 之间的 API 差异中看到 NS_REQUIRES_PROPERTY_DEFINITIONS。)

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 类的子类的实现中。

I could solve this problem simply by adding the @synthesize lines explicitly for the properties other than @dynamic in NSManagedObject subclasses.

我可以简单地通过为 NSManagedObject 子类中的 @dynamic 以外的属性显式添加 @synthesize 行来解决这个问题。