objective-c--@Property Detailed

Source: Internet
Author: User

Instance variable:
In fact, the explicit point is Ivar + setter + getter (instance variable + access method), but in OC the attribute is more literal this series of special keywords makes OC properties somewhat different.

Member properties We should all have used, for example now to define a car class with a name and speed member variable:

#import <Foundation/Foundation.h>@interface  car:nsobject{    @public      *name;    Nsinteger speed;} @end

There is an offset within the OC class that specifically marks the position of the member variable in memory. If you are now adding a new member variable in front of name, then there will be an overall low offset problem, and now add a price instance:

#import <Foundation/Foundation.h>@interface  car:nsobject{    @public     Nsinteger price;     *name;    Nsinteger speed;} @end

At this point the offset is shown in memory as follows:

Car Car
Name Price
Speed Name
Speed

You can see that the instance offset has changed, but OC takes the instance variable as a "special variable" used to store the offset, and the offset is found at run time, and the offset is always correctly located by a class object.

@Property

Using attributes is more abstract than member variables, and you can use setter and getter to do more processing of variables.

Say the attributes of a property

@synthesize keywords

This keyword specifies the instance variable name of the property, and automatically synthesizes the setter and getter methods according to the storage semantics (ReadWrite, ReadOnly) system, and of course, it can be handwritten to overwrite the system provided.

@dynamic

This keyword tells the compiler not to synthesize setters and getter methods for me, and these methods will be implemented by myself. Of course we can not implement this does not occur during the compile phase, until the runtime checks whether the setter and getter are implemented, and throws an exception if no implementation is implemented.

For example, all properties of Nsmanagedobject subclasses in CoreData are all dynamic tags, because some properties of subclasses are not

Attribute attributes (Semantics)

Attributes are classified into four categories:

1. Atomicity:

Atomicity refers to whether the property is synchronous, and most of the properties in OC are nonatomic (non-atomic), which is atomic if not written nonatomic. In theory, the atomic properties of the read and write will be synchronous, but OC Atomic does not necessarily determine the properties for synchronization, if you really want to synchronize operations, but also with a deeper synchronization lock API. And atomic will greatly affect the efficiency, so generally will write nonatomic.

2. Read/write permissions:

Read and write for readonly and readwrite two kinds, the first one in the system will only synthesize getter method, the latter will generate both setter and getter. If the property is set to the ReadOnly property, the property is not modifiable.

3. Memory Management semantics:

Assign: This method will only be used for simple assignment of "scalar type" (cgfloat or Nsinteger, etc.), and the ID type will be assign, so the agent delegate property in iOS will be marked with assign, such as:

@property (nonatomic, assign)   ID <UITableViewDataSource> dataSource; @property (Nonatomic, Assign)   ID <UITableViewDelegate>   delegate;

Strong: When you assign a value using this attribute instance variable, the old value is disposed and the new value is set, a strong reference is made to the object, and the MRC is the reference count of +1.

Weak: property indicates a "non-owning relationship" that neither releases the old value nor retains the new value. With MRC, the reference count is constant and the property is automatically set to nil when the object being pointed to is disposed. Here to say a little more, weak runtime implementation is done by the hash table, with the variable name to do the key, once found that the property refers to the object is released, immediately set to nil.

Unsafe_unretained: As with weak, the only difference is that when the object is freed, the property is not set to nil. So it's unsafe.

Copy: Similar to strong, but this property is copied with a new copy. In many cases, copy is used in order to change the value of the property in the way mutable (mutable type), which we do not know, and copy to generate an immutable copy to prevent it from being modified. If we implement the Setter method ourselves, we need to copy it manually.

4. Method Name:

Getter = <name>

Setter = <name>

The method name can be modified to the method name we synthesized, which can make the access method semantics more conform to the application scenario.

If you want to set properties in other properties, or if you want to conform to attribute properties, such as copy, we still have to copy the properties manually. In this case, it is necessary to manipulate the instance variables directly, instead of invoking the setter and getter.

Accessing instance variables directly inside the object as much as possible

First of all, why not use setter and getter in the constructor and destructor, because the setter and getter are the methods we have wrapped, it is possible to add some judgment, and if the subclass calls the constructor of the parent class and implements its own setter and getter, Then it is likely that there will be a problem.

Accessing an instance variable through a property uses the literal semantics of the attribute, which makes it more efficient to execute than directly invoking the instance variable, but it is easier to debug and control by using property access to intercept the property's KVO.

Generally within the class recommended setting use setter to get direct with instance variable.

Here again lazy loading, so-called lazy loading means that the property will be initialized the first time the getter is called, as follows:

-(NSString *) name{    if (!  _name) {        = [[NSString alloc] init];    }     return _name;}

Then it is only possible to invoke the instance variable by getter.

objective-c--@Property Detailed

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.