error: writable atomic property cannot pair a synthesized setter/getter with a user defined setter/getter

Viewed 35704

I recently tried to compile an older Xcode project (which used to compile just fine), and now I'm seeing a lot of errors of this form:

error: writable atomic property 'someProperty' cannot pair a synthesized setter/getter with a user defined setter/getter

The code pattern which causes these errors always looks like this:

// Interface:

@property (retain) NSObject * someProperty;

// Implementation:

@synthesize someProperty; // to provide the getter
- (void)setSomeProperty:(NSObject *)newValue
{
    //..
}

I can see why the error is being generated. I tell the compiler to synthesize my property accessors (both getter and setter), and then immediately afterward I override the setter manually. That code has always smelled a little off.

So, what is the proper way to do this? If I use @dynamic instead of @synthesize, I will have to write the getter as well. Is that the only way?

4 Answers

You need to implement the getter also. Example:

// Interface:

@property (retain) NSObject * someProperty;

// Implementation:

- (void)setSomeProperty:(NSObject *)newValue
{
    @synchronized (self)
    {
        // ...
    }
}

- (NSObject *)someProperty
{
    NSObject *ret = nil;

    @synchronized (self)
    {
        ret = [[someProperty retain] autorelease];
    }

    return ret;
}

For others who are getting this error not for the reason OP described, you likely have the same issue as me:

You have a @property with the same name as a -()method.

Something like this:

@property UIView *mainView;

-(UIView *)mainView;
Related