Xcode 4 automatically generates iVars when using @

2020-06-06 01:57发布

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.

4条回答
等我变得足够好
2楼-- · 2020-06-06 02:22

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.

查看更多
聊天终结者
3楼-- · 2020-06-06 02:24

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.

查看更多
趁早两清
4楼-- · 2020-06-06 02:43

This is part of the compiler, actually.

You can read that in the LLVM specification website.

查看更多
一纸荒年 Trace。
5楼-- · 2020-06-06 02:43

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.

查看更多
登录 后发表回答