user3457292
user3457292

Reputation:

How come ivars don't have attributes the way properties do

How come properties have user settable attributes for storage and lifetime, but ivars don't. Especially considering the fact that a property is backed by an ivar.

Upvotes: 0

Views: 39

Answers (3)

Ken Thomases
Ken Thomases

Reputation: 90551

Instance variables can have attributes, such as __strong, __weak, __unsafe_unretained, etc.

Also, as others have noted, properties are about behavior. They declare accessor methods and calling an accessor method is invoking behavior. Also the property is interface, while instance variables are implementation. As such, properties communicate the design contract to clients of the class. That's not necessary with instance variables, since clients should ideally be ignorant of implementation details.

Upvotes: 6

Tom Harrington
Tom Harrington

Reputation: 70946

Because the effect of a @property is basically a superset of what you get by declaring an instance variable. With @property you're telling the compiler to create an instance variable along with accessor methods. The extra details tell it how those accessors should work. With an instance variable you're just declaring the ivar and leaving it at that.

Upvotes: 2

Rob Napier
Rob Napier

Reputation: 299345

Properties are not always backed by an ivar. Properties are just promises to implement accessors. If you don't implement the accessors yourself, then the system will automatically write some for you (called "synthesizing"), and will create an ivar as an implementation detail. The attributes you're describing are instructions to the compiler about how you want the synthesized accessors written.

Prior to ObjC 2, we wrote all our accessors by hand. Properties and synthesized accessors were added later to automatically implement patterns that had been long established at that point.

Upvotes: 4

Related Questions