Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot override property getter with XCode 4.5

I am having some problems with some old sample code when using it with Xcode 4.5.

In my code I have the following property defined

@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;

Then I have the following accessor method:

- (NSManagedObjectModel *)managedObjectModel {
    if (_managedObjectModel != nil)
    {
        return _managedObjectModel;
    }

    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyPrototype" 
                                                   withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] 
                                          initWithContentsOfURL:modelURL];
    return _managedObjectModel;
}

The problem is that Xcode throws multiple errors as it cannot 'see' _managedObjectModel. If I change the name of the accessor from managedObjectModel to managedObjectModel2, everything works fine. I guess that the issue is related to Xcode 4.5 automatic property synthesizing, but I don't know what I should do to avoid the issue. Any suggestions?

like image 877
Huibert Avatar asked Dec 20 '12 16:12

Huibert


1 Answers

Clang won't automatically synthesize an instance variable for you if it doesn't have any methods to generate. In this case, you've asked for a readonly property, so there's no setter, and you've providing the getter. You can just @synthesize managedObjectModel=_managedObjectModel or just declare the instance variable yourself.

like image 89
Jesse Rusak Avatar answered Sep 30 '22 18:09

Jesse Rusak