The usage of @property and @synthesize in Objective-c

Source: Internet
Author: User

The usage of @property and @synthesize in Objective-c

1. Keywords @property and @synthesize are paired in Obj-c and are used to simplify the coding of declarations and implementations;
eg
declaration in the header file (. h)
@property int personage;
Equivalent to:
-(int) personage;
-(void) Setpersonage: (int) age;

File (. m) Implementation
@synthesize personage;
Equivalent to:
-(int) personage
{
return personage;
}
-(void) Setpersonage: (int) Age
{
Personage = age;
}

2. The syntax for declaring property is: @property (parameter 1, parameter 2) type name;
eg
@property (readwrite,copy) NSString *personname;
The parameters are mainly divided into 3 categories:
<1>. Accessor control: (readwrite/readonly)
&LT;2&GT; memory management: (assign/retain/copy/strong/weak/unsafe_unretained)
<3> atomicity: (Atomicity/nonatomic)
ReadWrite
Declares that this property is read-write, that is, you can access the Set method (setter), or you can access the Get method (getter), which is mutually exclusive with ReadOnly.
ReadOnly
Declares that this property is read-only and can only access the Fetch method (getter) corresponding to this property, which is mutually exclusive with ReadWrite.
Assign
The default type, used for value types (int, float, double, and nsinteger,cgfloat for simple replication), also includes objects that do not have a ownership relationship (common such as delegate), declared in the setter method, with direct assignment to implement the set-value operation. eg
-(void) Setpersonname: (NSString *) name
{
PersonName = name;
}
Retain
means that the instance variable takes ownership of the passed parameter, and declares that in the setter method, the set value needs to be retain plus 1 operations. eg
-(void) Setpersonname: (NSString *) name
{
if (personname! = name)
{
[PersonName release];
PersonName = [name retain];
}
PersonName = name;
}
Copy
Call the copy method of this instance to set the cloned object. Implement reference retain.
Strong
The keyword introduced when arc was introduced with iOS is an optional alternative to retain. Indicates that the instance variable has a ownership relation to the passed-in parameter, which is a strong reference. Strong and retain have the same meaning and produce the same code, but the semantics better reflect the relationship of the object.
Weak
Similar to the Assign effect, the difference is that weak is automatically set to nil after the object is recycled. and weak smart is used in iOS 5 or later versions, for previous versions, using unsafe_unretained.
unsafe_unretained
Weak the lower version of the replacement.
Nonatomic
By default, the setter and getter implemented through synthesized are atomically accessed.
When multithreading is accessed at the same time, the guaranteed access method is accessed only by one thread, eg:
[_internal Lock]; Lock using an object-level lock
ID result = [[value retain] autorelease];
[_internal unlock];
return result;
However, if Nonatomic is set, access to the property is non-atomic, but is faster than atomic and is widely used in single-threaded and explicitly only one-thread access scenarios.

[Email protected]
For the following @propety
@property (nonatomic, copy) NSString *personname;
The rules for the default composition are:
@synthesize personname = _personname;
Where _personname is the compiler automatically help us to create;
The name of a getter and setter that is not the same as the variable name can also be defined in the @synthesize to protect the variable from inappropriate access.

The usage of @property and @synthesize in Objective-c

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.