The relationship of the instance variable (instance var) to the attribute (@property)

Source: Internet
Author: User

The relationship of the instance variable (instance var) to the attribute (@property)

After Objective-c 2.0, declaring a @property name automatically produces an instance variable named _name, eliminating the hassle of repeating input for instance variables and properties. You can change the _name name by using @synthesize. @property and @synthesize do not have to appear in pairs.

@property Name: Instructs the compiler to automatically synthesize the setter and getter methods, the setter method name is SetName, and the Getter method name is name. @property The following keywords, such as ReadOnly, ReadWrite, retain, nonatomic, copy, and so on, to specify how the setter and Getter methods are produced. These property modifiers are broadly divided into four categories:

(1) variability (mutability)

-ReadOnly, Generate Getter method only, no setter method

-ReadWrite, is the default

(2) Memory management (management)

-Assign, is the default, applies to built-in types (int, bool, etc.) or proxy objects (delegate), there is no reference counting mechanism.

-Retain, applies only to objects, does not apply to built-in types (int, bool, etc.). When using the Setter method, add 1 to the object's reference count.

-Copy, when using the setter method, copies an object that creates a new object in memory instead of adding 1 to the reference count of the original object. Obviously, the reference count for the new object that was copied is 1.

(3) Concurrency (Concurrency)

-Nonatomic, Access attribute non-atomicity, general single-threaded declaration nonatomic, taking into account the speed problem. Do not use nonatomic for multi-threaded threads.

-Atomic, Access attribute atomicity, opposite to nonatomic.

(4) API controls (API control)

-Getter=newgettername, which specifies the new getter method name, typically rewrites the getter name of the BOOL instance variable. For example

[OBJC]View Plaincopy
    1. @property (getter=isfinished) BOOL finished;

-Setter=, specifying the new setter method name.

@synthesize name = Custom_name: Replace the instance variable _name name with Custom_name

@synthesize Name: Replace the instance variable _name

Note: @synthesize does not affect the name of the setter and getter methods produced by @property

When is the setter and getter method called?

For example, attribute declarations are as follows

[OBJC]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. @interface Person:nsobject
    2. @property NSString *firstname; //Atomic, assign, ReadWrite (default)
    3. @end

There are 2 ways to do this:

(1) Show call (Send message)

[OBJC]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. [Someperson FirstName]; //Call getter Method
    2. [Someperson Setfirstname: @ "Johnny"]; //Call setter method

(2) Implicit invocation (point syntax)

[OBJC]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. NSString *firstname = Someperson. firstName; //Call getter Method
    2. Someperson. firstName = @ "Johnny"; //Call setter method

If you use instance variables directly in an instance method, the corresponding setter and getter methods are not called, for example

[OBJC]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. -(void) SomeMethod
    2. {
    3. nsstring *mystring = _firstname; //Won ' t call getter method
    4. _FirstName = @ "A string"; //Won ' t call setter method
    5. }

Obviously using instance variables directly can be risky, such as memory leaks, circular references, and so on. The best way to access instance variables is through @property generated setter and getter methods.

The relationship of the instance variable (instance var) to the attribute (@property)

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.