Pergunta

My professor has explicitly told us several times that in Xcode 4.6 calling:

@synthesize suit;

is the same as calling:

@synthesize suit = _suit;

in that Xcode will create an ivar and name it with the underbar naming convention (example @property suit will get an ivar named _suit)

However, I have been looking at documentation and found this passage in the Apple Documentation EncapsulatingData

Important: If you use @synthesize without specifying an instance variable name, like this: @synthesize firstName; the instance variable will bear the same name as the property. In this example, the instance variable will also be called firstName, without an underscore.

This appears to directly contradict my professor. :(

Here is what I know. In Xcode 4.6 any @property declared will get @synthesize-ed automatically so you wouldn't even need to call @synthesize. If you allow Xcode to auto synthesize it does use the ivar underbar name (_suit). However if you implment custom getters and setters you have to call @synthesize explicitly.

My question is this. When you have a custom getter and setter (as I have) and need to call @ synthesize does Xcode use the default underbar naming scheme when you don't specify the name?

For example when I call this:

@synthesize suit;

Does Xcode name the underlying ivar suit or _suit?

From my simple tests and reading the docs it appears that @synthesize suit; becomes @synthesize suit = suit and not @synthesize suit = _suit as my professor thinks it does. I am a total rookie, just wrapping my head around properties and synthesizing so I need your help.

Also, what could I do to test this myself or find the actual answer in the docs? Thanks.

Foi útil?

Solução

Here is the official word - Programming with Objective C Encapsulating Data, and perhaps also the source of confusion

Most Properties Are Backed by Instance Variables

Unless you specify otherwise, the synthesized instance variable has the same name as the property, but with an underscore prefix. For a property called firstName, for example, the synthesized instance variable will be called _firstName.

Further down the same page (the part you quoted)...

You Can Customize Synthesized Instance Variable Names

Important: If you use @synthesize without specifying an instance variable name, like this:
@synthesize firstName;
the instance variable will bear the same name as the property.
In this example, the instance variable will also be called firstName, without an underscore.

They do almost seem to contradict themselves, but the earlier paragraph is only referring to autosynthesized instance variables, while the latter is what happens when you explicitly synthesize.

Autosynthesis is a much newer feature which only came to XCode in version 4.4 with the inclusion of Clang compiler v4. Prior to this there were endless debates about the pros and cons of @synthesizing your variables with a leading undersore (legend had it that this was reserved for Apple's use), a trailing underscore (Google's objective-C style guide) or neither. Since XCode 4.4 this is settled: name all your iVars explicitly with a leading underscore, but better still leave the compiler do it for you.

update: I noticed Google has updated their style guide to fit in with the rest of us...

Trailing underscores were once preferred for instance variable names.

Our style guide used to have a rule saying that instance variables should be named with a trailing underscore, similar to the naming of member variables in C++. This was changed to leading underscores to be consistent with the broader Objective-C community, to better follow Apple's official guidelines, and to allow for use of new compiler features like automatic instance variable synthesis. New projects are strongly encouraged to use leading underscores. Existing projects may continue to use trailing underscores in new code to maintain consistency with the rest of their codebase.

Outras dicas

You are right (and your professor is not). @synthesize creates an ivar with the same name as the property if the variable name is not specified.

@synthesize foo;

...will create an ivar named foo

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top