Search code examples
objective-cxcodeautocompleteivar

Objective-C: Xcode automatically recognizes ' = _property ": is this @synthesize created variable name?


When you declare a @property and @synthesize it, it is considered good practice to use:

@synthesize myProperty = _myProperty;

I've noticed that Xcode will autocomplete the ivar name _myProperty for you, even though it hasn't yet been used in the source code.

Is this because the ivar @synthesize creates automatically defaults to the name _myProperty? Or merely because Xcode supports this common convention with an autocompletion for it?

Thanks.

EDIT: I'm not looking for reasons why this is good practice; I'm already aware of those and have used this convention for a while. I want to understand the internals, thus am asking whether this is a hard-coded auto-completion rule to satisfy a convention, or whether it's standard auto-completion and in fact the Objective-C specification dictates that an ivar generated by @synthesize must have the form _myProperty, thus after behind the scenes generation of the ivar, auto-completion is aware of its existence. Thanks!


Solution

  • I think the autocompletion is an IDE convenience rather than a result of the runtime. My logic for this is that the following appears to be valid:

    @interface SomeClass()
    @property (nonatomic, assign) int unpublishedInstanceVariable;
    @end
    
    @implementation SomeClass
    @synthesize unpublishedInstanceVariable;
    
    - (void)someMethod
    {
        unpublishedInstanceVariable = 3; // not calling the setter
    }
    @end