Li Fumin
Li Fumin

Reputation: 1453

Xcode 4 automatically generates iVars when using @property,where can I find the official doc for this feature?

I have read "What's new in Xcode",but I can't find the official explanation for this feature. Where can I find the official explanation? Which documentation? Thanks.

Upvotes: 3

Views: 1327

Answers (4)

rob mayoff
rob mayoff

Reputation: 385600

Assuming you mean that it auto-generates an ivar and getter and setter methods for you even if you omit the @synthesize: this is variously called default property synthesis, automatic property synthesis, and property autosynthesis.

There's not a lot of documentation. As far as I have found, there's no official documentation of how it works, just of the fact that it exists.

It's really a clang feature, not an Xcode feature. It appeared briefly in the version clang shipped with Xcode 4.0 DP 4, but was removed shortly after due to bugs. It reappeared in the version of clang shipped with Xcode 4.4. Here's the commit that added it, I think.

You can find it mentioned in the Objective-C Feature Availability Index.

It's also mentioned in the Clang Language Extensions.

From experimentation:

  • If you don't explicitly @synthesize a property and it generates an instance variable, it will automatically generate an ivar with the same type (and, under ARC, ownership qualification) as the declared property. The ivar name will be an underscore (_) followed by the declared property name.

  • If you don't explicitly @synthesize a readonly property, and you do include an explicit getter method, then clang will not automatically generate an ivar for you.

  • If you don't explicitly @synthesize a readwrite property, and you do include both an explicit getter and an explicit setter, then again clang will not automatically generate an ivar for you.

But I don't know of any official documentation of these behaviors.

Upvotes: 14

torrey.lyons
torrey.lyons

Reputation: 5589

You can find this in the Apple documentation in Objective-C Programming Language: Declared Properties under "Property Implementation Directives". Whether or not an ivar is synthesized automatically depends on what runtime you are using:

There are differences in the behavior of accessor synthesis that depend on the runtime (see also “Runtime Difference”):

  • For the legacy runtimes, instance variables must already be declared in the @interface block of the current class. If an instance variable of the same name as the property exists, and if its type is compatible with the property’s type, it is used—otherwise, you get a compiler error.
  • For the modern runtimes (see “Runtime Versions and Platforms” in Objective-C Runtime Programming Guide), instance variables are synthesized as needed. If an instance variable of the same name already exists, it is used.

iOS always uses the modern runtime so you never need to declare ivars explicitly.

Upvotes: 8

Rob
Rob

Reputation: 437552

I would also draw your attention to the Coding Guidelines for Cocoa which advises:

  • Avoid explicitly declaring public instance variables.

    Developers should concern themselves with an object’s interface, not with the details of how it stores its data. You can avoid declaring instance variables explicitly by using declared properties and synthesizing the corresponding instance variable.

Upvotes: 1

Mazyod
Mazyod

Reputation: 22559

This is part of the compiler, actually.

You can read that in the LLVM specification website.

Upvotes: 2

Related Questions