Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type of property does not match type of accessor

Using Xcode 6 Beta 4, targeting 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:

@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.

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

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

I get this warning:

Type of property 'objectID' does not match type of accessor '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.

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)

What's 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.

like image 343
kylejm Avatar asked Jul 26 '14 15:07

kylejm


1 Answers

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:

- (NSManagedObjectID *)objectID;

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

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

As explained in https://stackoverflow.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

- (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).

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

- (MyEntityID*)objectID;

by

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

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

@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.

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.

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

like image 140
Martin R Avatar answered Oct 26 '22 11:10

Martin R