IOS Intro - Property Synthesis

Posted fanbird2008

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IOS Intro - Property Synthesis相关的知识,希望对你有一定的参考价值。

 

 

http://www.thecodecrate.com/ios/objective-c/objective-c-property-synthesize/

 

 

01. atomic                   // default
02. nonatomic
03. strong=retain            // default
04. weak= unsafe_unretained
05. retain
06. assign                   // default
07. unsafe_unretained
08. copy
09. readonly
10. readwrite                // default

 

Objective-C @property and @synthesize

技术分享
 
 
技术分享
 
0

技术分享In Objective-C, we very often use the following directives – @property and @synthesize, but sometimes it can get a little blurry on why we need them.  What are they used for and what do they actually do?  In fact, do they have to be used at all?  I’ll explain how these work together and what the Objective-C compiler does with them…

 

Use accessor methods to Get and Set property values

So, if we have this line of code in the @interface section in the .h file

This defines a public class property.  This means that the class has a property named ‘firstName’ and the compiler will create a backing instance variable (ivar) for this property for you.  Note, the instance variable that this property is accessing need not necessarily be named ‘firstName‘.

So, if we only have the @property declaration (without specifying the @synthesize keyword), in Xcode 4.4 and LLVM 2.0, the compiler will automatically synthesize (create the getter and setter methods as well create the backing ivar for it), and the property will reference the instance variable.  This auto-synthesize step creates an instance variable that will be the property name prefixed with an underscore ‘_’.   The compiler will synthesize these ‘setter’ and ‘getter’ methods for us, but they won’t appear in the code for us to see.  However, what the compiler creates on our behalf looks something like this — maybe not exactly, but close enough:

So the property firstName will access the instance variable ‘_firstName’.  This naming convention for the instance variable is automatically created by  the compiler.  By the way, notice that the getter method has the same name as the property, and the setter method starts with the word set and then uses the capitalized property name, i.e., the methods that are created by the compiler will be firstName for the ‘getter’ method, and setFirstName for the ‘setter’ method.

This allows us to write the following code that reads and writes the name in the ‘PersonClass’ class.

 

Using @synthesize

If we do specify @synthesize in the implementation like so:

then the compiler will also create the “setter” and “getter” methods for us, but the backing ivar that will be created for us will be named firstName.  This time, the property name and the instance variable will have the same name.  The compiler will create “setter” and “getter” methods that look like this:

We can also specify @synthesize in the implementation as:

The compiler will create an instance variable called _my_firstName for the firstName property.  Whenever the variable is referenced, we need to use the _my_firstName naming convention.  This allows us to customize synthesized instance variable names.  Now, the compiler will create “setter” and “getter” methods that look like this:

Why do we want to do all of this?

Well, for starters, if we can get the compiler to name all our instance variables to have an prefixed underscore (or whatever naming convention we choose), then it’ll allow us to differentiate between instance variables and local variables in the class implementation.

 

Specifying accessors methods

By default, both the setter and getter accessor methods will be created.  We can however specify which accessor methods we want the compiler to create for us.  If we don’t want an ivar to be changed  using a setter method, we can add an attribute to the property indicating whether it should be readonly, like so:

The compiler will synthesize a getter method, but not a setter method.

If we specify @synthesize, remember I mentioned that the name of the accessor methods will be the same as the name of the property.  Well, we can indicate to the compiler that we’d like to change the name of the accessor methods that it creates.  One common example is a BOOL property. It’s customary to create a BOOL getter method that starts with the word ‘is’, and the setter method to have the same name as the property itself.  For example:

This will create the following getter and setter methods:

 

Overriding accessors methods

By default, both the setter and getter accessor methods will be created.  We can however override one of them if we choose to – perhaps we need additional functionality in either the getter or setter method that the compiler cannot provide.  Either way, if we override only one accessor method (irrespective which one), the compiler will still create the other accessor method.  So if we have something like this:

The following code will work perfectly well:

If however, we override both accessor methods, then the compiler steps back and lets us do all the hard work.  It does not create any setter or getter methods, because we decided to override them, and it doesn’t create any backing ivars for the property either.  So, we have to provide all the code, as follows:

We still have to use the @synthesize (1) directive so that the backing ivar is created.  If we don’t use it, then we have to define the instance variable in the @interface section like this …

which brings us back to pre-Xcode 4.4 without the auto-synthesize change.

That’s all for now regarding the synthesized accessor methods, hope you’ve found this useful.  Thanks for reading…

---恢复内容结束---










以上是关于IOS Intro - Property Synthesis的主要内容,如果未能解决你的问题,请参考以下文章

IOS Intro - NSDictionary and NSMutableDictionary

Objective-C中的@dynamic(转)

使用面临问题 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL synt

iOS-关于@property和@synthesize

iOS 深入理解 @property

需要解释 iOS 中的 @property/@synthesize