2015.12.18 Property Definition attribute Variable init Initializes a partial use of the custom class

Source: Internet
Author: User

Property

In the previous study and application, because the member variables are private by default (private), the definition of a variable will be handwritten a "getter" and "setter" code. But because it is too cumbersome to write, it introduces the property (declaration) and synthesize (implementation). And then, synthesize was omitted.

Eg: @property (nonatomic, strong)nsstring *name;

Here, "property" has done three tasks for us: 1. Declares an attribute variable

2. The setter, getter method is declared

3. Implements setter, Getter method

The keywords in the property:

Atomic atomic Operation thread safety with shackle unlock mechanism security (default)

Nonatomic Non-atomic operation thread Unsafe speed high efficiency (general use)

Strong declares ownership of an object that is not created by itself

If you discard ownership after you create the ownership, the system automatically frees the memory after you discard the ownership and do not release the memory.

Prevent a after creating the object B to use, a gave up, B can no longer find this situation.

Like retain and strong.

Weak only use without generating ownership

Copy re-copying objects The advantage of being out of the relationship with the original object is that the memory is increased.

The difference from strong is whether to break out

Assign for the basic data type of C language with assign

ReadOnly read-only equivalent to provide getter method only

ReadWrite Read-Write default

Init

Init custom purpose is equivalent to the construction method, sometimes there are some default values when initializing, there is the time when the page load data, prevent loading the page refill data (out of sync).

eg

-(Instancetype) Initwithname: (NSString *) aName andage: (int) aage{            if (self =[super Init]) {       // Call the parent class method Init and determine if initialization succeeds          = aName;                = AAge;    }         return self ; }

  Three return types can be used with the same meaning when customizing

-(person *) initwithname

-(ID) initwithname//ID is the abbreviation of identify, in some degree equivalent to void

-(Instancetype) Initwithname//is the type of init in NSObject, it is recommended to use

class

@class class name;--solve circular reference problems, improve performance

@class just tell the compiler to handle the following name as a class when compiling.

(1) The role of @class: Declaring a class that tells the compiler that a name is a class

(2) Specification of a class referenced in development

1) Declare the class using @class in the. h file

2) Use #import to include everything in the class when you really want to use it in. m files

P.S. For example, a lock is included in a key class and a key is included in the lock. Mutual #import will go wrong because they always quote each other.

2015.12.18 Property Definition attribute Variable init Initializes a partial use of the custom class

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.