objective-csynthesize

Is overriding a synthesize property in Objective-C Ok or bad?


I have overridden a synthesized property because I wanted to add an NSAssert.

Is this OK to do (i.e override) or considered bad practice?

@synthesize someField;

-(NSString*)someField {
    NSAssert(someField != nil,@"someField");
    return someField;
}

Solution

  • That is fine. From the documentation:

    You use the @synthesize keyword to tell the compiler that it should synthesize the setter and/or getter methods for the property if you do not supply them within the @implementation block.

    So if you provide them, then the compiler will use yours, irrespective of the @synthesize directive.