Example 1 demonstrates an old way of defining pairs of ivar / property variables. The new compiler now generates ivars (part of NSstring *_sale; ) for you. Example 1 also demonstrates manually matching the sale property with ivar _sale using the @synthesize sale = _sale; .
Example 2 is a more concise way to implement properties in Obj-C, and this is how you will see most code examples on the Internet. The vast majority of the time, you can write your properties without overwriting the accessor / mutator methods that the compiler created for you.
There are some strong proponents of the underscore prefix to refer to instance variables for clarity. You may find that this helps you when it comes to memory management, as in Example 1, setting a self.sale equal to an autorexized NSString will be fine as it will be saved, but setting a _sale equal to an autorealized object will result in a more unstable behavior later because the passed NSString will not be saved by the instance variable.
In general, I prefer to write my properties, as shown in Example 2.
The short answer . There are two ways to do this, because the new compiler can now output some things for you, but the previous way to do everything has remained for backward compatibility.
source share