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

StackOverflow https://stackoverflow.com/questions/7741313

سؤال

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!

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

hard-coded auto-completion rule to satisfy the convention

If you don't specify an iVar name explicitly, it will be called myProperty. The autocomplete doesn't have anything to do with the compiler, it's just Xcode being extra helpful.

As of Xcode 4.4, there is a new twist to the tail (sic).

  • We are now allowed to skip the @synthesize altogether. In this case, the compiler automatically generates the @synthesize foo = _foo; declaration for us, with the instance variable name prefixed with an underscore.

@interface Foo : NSObject
@property (nonatomic, copy) NSString *foo;
@end

@implementation Foo
- (void)bar {
    NSLog(@"%@", _foo); // this Works!
}   
@end
  • However, if we do have an explicit @synthesize statement but do not specify the name of the instance variable, then the default name of the instance variable is the same as that of the property i.e. not prefixed with an underscore, in which case @Tommy's answer still holds.

It'd be great if someone could point out the links to official Apple Docs that document this behaviour.

Update

My findings were spot on. This behaviour (of @synthesize being the default, and creating a backing underscore prefixed instance variable in the absence of an explicit synthesize etc.) was publicly announced in WWDC 2012 Session 405 - Modern Objective-C.

wrt this part of your question:

whether ... in fact the Objective-C specification dictates that an ivar generated by @synthesize must have the form _myProperty,

You can name your ivar anything you want. From the docs:

You can use the form property=ivar to indicate that a particular instance variable should be used for the property, for example:

@synthesize firstName, lastName, age=yearsOld; 

This specifies that the accessor methods for firstName, lastName, and age should be synthesized and that the property age is represented by the instance variable yearsOld.

Also,

The @synthesize directive also synthesizes an appropriate instance variable if it is not otherwise declared.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top