属性的类型与访问器的类型不匹配

时间:2022-09-06 21:00:10

Using Xcode 6 Beta 4, targeting iOS 8:

使用Xcode 6 Beta 4,针对iOS 8:

I'm using Core Data and Mogenerator together. If you're not familiar with "Mogen", skip to the bottom. When Mogen generates a NSManagedObject subclass for an entity, it does this:

我把Core Data和Mogenerator放在一起。如果你不熟悉“Mogen”,那就直接跳到下面。当Mogen为一个实体生成一个NSManagedObject子类时,它会这样做:

@interface MyEntityID : NSManagedObjectID {}
@end

@interface _MyEntity : NSManagedObject {}

- (MyEntityID*)objectID;

----------------------------------------------------------------------------

@implementation _MyEntity

- (KJMWorkoutID*)objectID {
    return (KJMWorkoutID*)[super objectID];
}

It's kind of handy I guess. NSManagedObject has an objectID property, all _MyEntity is doing is overriding it's getter to return a MyEntityID so that we can tell it's an ID specifically for our _MyEntityClass.

我想这很方便。NSManagedObject有一个objective属性,所有_MyEntity所做的就是重写它的getter来返回MyEntityID这样我们就可以知道它是_MyEntityClass的一个ID。

So, back to my question. I want to compare two MyEntityIDs by checking that they are not equal:

回到我的问题。我想通过检查两个MyEntityIDs是否不相等来比较:

if (![self.objectID isEqual:self.previousID])

I get this warning:

我得到这个警告:

Type of property 'objectID' does not match type of accessor 'objectID'

属性'objectID'的类型与访问器'objectID'的类型不匹配

Okay, I understand that the property is NSManagedObjectID, but we're calling the accessor method Mogen has written which returns MyEnytityID. Even the code completion recognises this.

我知道属性是NSManagedObjectID,但是我们调用了Mogen写的accessor方法它返回MyEnytityID。即使代码完成也承认这一点。

How is it still seeing the NSManagedObjectID property type for objectID of NSManagedObject? Should I silence the warning somehow? Is it likely just a Xcode 6 Beta thing? (it doesn't happen in Xcode 5)

它怎么还能看到nsmanagedobectid的属性类型呢?我应该以某种方式让这个警告安静下来吗?它可能只是Xcode 6测试版吗?(Xcode 5中没有)

What's Mogen:

Mogen:

Mogen is just a helpful tool that generates subclasses of NSManagedObject for your data model entities. It's kind of like what Xcode does, but doesn't erase any custom code you write every time you re-generate the subclasses and it gives you a whole bunch of nice methods to use for creating new entities etc. If the under bar prefixed class name thing there is something new to you too, it's just the way that Mogen stops itself from overwriting any custom code you write.

Mogen只是一个有用的工具,它为数据模型实体生成NSManagedObject的子类。有点像什么Xcode,但不删除任何您编写自定义代码每次重新生成子类,它给你一大堆的好方法用于创建新实体等等。如果在酒吧前缀类名称的事情有新的东西你也只是Mogen阻止本身覆盖的方式编写任何自定义代码。

2 个解决方案

#1


38  

The problem is unrelated to the Xcode 6 beta release, but due to the fact that the declaration of objectID changed in iOS 8. Up to iOS 7, it was declared as an instance method:

这个问题与Xcode 6测试版无关,但由于在ios8中objective的声明发生了变化。在ios7之前,它被声明为一个实例方法:

- (NSManagedObjectID *)objectID;

As of iOS 8/OS X 10.10, it is declared as a read-only property:

在iOS 8/OS X 10.10中,它被声明为只读属性:

@property (nonatomic, readonly, strong) NSManagedObjectID *objectID;

As explained in https://*.com/a/7086119/1187415, you can override an instance method in a subclass with a method having a more specialized return value, in this case

正如https://*.com/a/7086119/1187415中所解释的,您可以在子类中使用具有更特殊返回值的方法覆盖实例方法,在本例中是这样

- (MyEntityID *)objectID;

But is seems to be problematic to override a property with a more specialized return value (and doing so would cause the identical warning with Xcode 5).

但是,使用更特殊的返回值覆盖属性似乎有问题(这样做将导致与Xcode 5相同的警告)。

The following seems to work with both the "old" and the "new" SDKs: In "_MyEntity.h", replace

以下似乎同时适用于“旧的”和“新的”sdk:在“_MyEntity”中。h”,取代

- (MyEntityID*)objectID;

by

通过

@property (nonatomic, readonly, strong) MyEntityID *objectID;

and in "_MyEntity.m", remove the - (MyEntityID*)objectID method and add

在“_MyEntity。删除- MyEntityID*)objectID方法并添加

@dynamic objectID;

However, these files are always re-created by Mogenerator, so this is not a satisfying solution. I would recommend to file a bug report to the Mogenerator people to find a solution that is compatible with the iOS 8/OS X 10.10 SDKs.

但是,这些文件总是由Mogenerator重新创建,所以这不是一个令人满意的解决方案。我建议向Mogenerator人员提交一个bug报告,以找到与iOS 8/OS X 10.10 SDKs兼容的解决方案。

I could not find a compiler option to suppress this warning (it is not marked with "-W..." so that a "-Wno..." option would suppress it.

我找不到一个编译器选项来抑制这个警告(它没有标记为“-W…”,以便“-Wno…”选项可以抑制它。

As a workaround, you can replace self.objectID by [self objectID], which does not cause the warning.

作为一个解决方案,你可以替换self。objective by [self objective],不会引起警告。

#2


3  

@Martin R's answer is correct, but for those arriving here from now on, Mogenerator has been updated to v1.28 and fixes this issue.

@Martin R的答案是正确的,但是对于从现在开始到达这里的人,Mogenerator已经更新到v1.28并修复了这个问题。

#1


38  

The problem is unrelated to the Xcode 6 beta release, but due to the fact that the declaration of objectID changed in iOS 8. Up to iOS 7, it was declared as an instance method:

这个问题与Xcode 6测试版无关,但由于在ios8中objective的声明发生了变化。在ios7之前,它被声明为一个实例方法:

- (NSManagedObjectID *)objectID;

As of iOS 8/OS X 10.10, it is declared as a read-only property:

在iOS 8/OS X 10.10中,它被声明为只读属性:

@property (nonatomic, readonly, strong) NSManagedObjectID *objectID;

As explained in https://*.com/a/7086119/1187415, you can override an instance method in a subclass with a method having a more specialized return value, in this case

正如https://*.com/a/7086119/1187415中所解释的,您可以在子类中使用具有更特殊返回值的方法覆盖实例方法,在本例中是这样

- (MyEntityID *)objectID;

But is seems to be problematic to override a property with a more specialized return value (and doing so would cause the identical warning with Xcode 5).

但是,使用更特殊的返回值覆盖属性似乎有问题(这样做将导致与Xcode 5相同的警告)。

The following seems to work with both the "old" and the "new" SDKs: In "_MyEntity.h", replace

以下似乎同时适用于“旧的”和“新的”sdk:在“_MyEntity”中。h”,取代

- (MyEntityID*)objectID;

by

通过

@property (nonatomic, readonly, strong) MyEntityID *objectID;

and in "_MyEntity.m", remove the - (MyEntityID*)objectID method and add

在“_MyEntity。删除- MyEntityID*)objectID方法并添加

@dynamic objectID;

However, these files are always re-created by Mogenerator, so this is not a satisfying solution. I would recommend to file a bug report to the Mogenerator people to find a solution that is compatible with the iOS 8/OS X 10.10 SDKs.

但是,这些文件总是由Mogenerator重新创建,所以这不是一个令人满意的解决方案。我建议向Mogenerator人员提交一个bug报告,以找到与iOS 8/OS X 10.10 SDKs兼容的解决方案。

I could not find a compiler option to suppress this warning (it is not marked with "-W..." so that a "-Wno..." option would suppress it.

我找不到一个编译器选项来抑制这个警告(它没有标记为“-W…”,以便“-Wno…”选项可以抑制它。

As a workaround, you can replace self.objectID by [self objectID], which does not cause the warning.

作为一个解决方案,你可以替换self。objective by [self objective],不会引起警告。

#2


3  

@Martin R's answer is correct, but for those arriving here from now on, Mogenerator has been updated to v1.28 and fixes this issue.

@Martin R的答案是正确的,但是对于从现在开始到达这里的人,Mogenerator已经更新到v1.28并修复了这个问题。