问题描述:

This question already has an answer here:

  • Is there a difference between an “instance variable” and a “property” in Objective-c?

    5 answers

网友答案:

Objective-c use to be simple...and tedious. You would declare instance variables for a class (the orange) and then you would define (usually) 2 methods for each, one so that an external class could set each instance variable to a new value, and one that returned the ivars value so an external object could read it. Aka, you had to write a getter and setter for each ivar. This was two lines of code in the interface and sometimes around 10 lines of code for the implementation file.

Then came properties, declared with @property. There was much rejoicing and drinking in the streets. These single @property lines told the compiler to write those methods for you, including the correct memory management code and even mutex locking code (depending on what you specified when declaring the @property)

Theres a whole lot of history, but nowadays, with automatic reference counting, it really only makes sense to use @properties in your interface, when you want to make an ivar public, and declare your private ivars in your implementation file.

Lastly, not that @property not only tells the compiler to implement your getter and setter, but it also automatically provides an instance variable with the same name, but prefixed with an underscore (this is only if you have implicit synthesizing of properies enabled...more history)

So, thats the difference. @property tells the compiler to write code for you (essentially). What code it actually writes is modified by all the different ways you can declare a @property.

网友答案:

The red set is properties, and the orange set is instance variables.

A property declaration tells the compiler to define a getter method, and possibly a setter method. (No setter method if the property is readonly.)

In newer versions of Objective C, declaring a property also creates an instance variable that is used to save values for the property. By convention the instance variable has the same name as the property, but with an "_" prefix. There is a way to change the name of the instance variable, but let's ignore that for now.

The property foo:

@property (nonatomic, strong) NSString *foo;

Would have a getter method:

- (NSString *) foo;

and a setter method

- (void) setFoo: (NSString *) foo;

That enables you to use code like this:

NSString *aString = self.foo;

or

NSString *aString = [self foo];

(2 different, equally valid ways of invoking the getter)

And invoking the setter

self.foo = @"a string";

or

[self setFoo: @"a string"];

(2 different, equally valid ways of invoking the setter)

Properties are really useful when you want to create a public interface to get and set values in your class from outside. If you declare a property as "atomic" the compiler adds additional code to the getter and the setter so reads and writes to the property are "thread safe", and can be accessed from background threads.

Before ARC, properties also were a very clean way to manage retains and releases. You declared a property as "retain" and the setter was written to retain the object that was passed in. That's less of an issue in ARC, because the system takes care of retains and releases for you.

It is also possible to write a custom getter or setter method that invokes your own code instead of the compiler-written code. You can use that to do things like log information, send notifications about changes, update labels, etc, etc. You simply add a method body to your .m file that has the same method signature as the getter or setter and the compiler uses that method instead of the automatically generated one.

As I said before, the code:

self.foo = @"a string";

is the same as

[self setFoo: @"a string"];

and invokes the setter method. The setter method sets the internal instance variable _foo.

However, the code

_foo = @"a string"; 

changes the instance variable directly, without invoking the setter. If you do define a property, you should use it instead of the instance variable.

相关阅读:
Top