Dark Horse Programmer -- @ property and Its Parameter Details, dark horse @ property

Source: Internet
Author: User

Dark Horse Programmer -- @ property and Its Parameter Details, dark horse @ property
------ Java training, Android training, iOS training, and. Net training. We look forward to communicating with you! ------ @ Property 1. What is @ property?

@ Property is a keyword exclusive to OC. It is a feature of the compiler. When the compiler encounters @ property, it automatically expands to the set and get methods of member variables.

Before Xcode4.5, The Xcode compiler will convert @ property to the declaration of the set and get methods of the member variables. When @ synthesize is used to implement the set and get methods of the member variables, therefore, before XCode4.5, @ property and @ synthesize were paired. After Xcode4.5, Xcode will automatically convert the @ property keyword to (Declaration of member variables, declaration and implementation of corresponding set and get methods ).

2. How do we use @ property?

Code explanation:

Code we manually write (disable the ARC mechanism ):

@interface Person : NSObject{    NSString *_name;}- (void) setName:(NSString *)name;- (NSString *) name;@end@implementation Person-(void)setName:(NSString *)name{    if (_name != name) {        [_name release];        _name = [name retain];    }}-(NSString *)name{    return _name;}-(void)dealloc{    [_name release];    [super dealloc];}@end
Code written by pairing @ property with @ synthesize before Xcode4.5

// @ Interface Person: NSObject @ property NSString * name;/** @ property NSString * name; the sentence is expanded into: * {* NSString * _ name; *} *-(void) setName :( NSString *) name; *-(NSString *) name; */@ end @ implementation Person @ synthesize name = _ name; // you must write = _ name here; otherwise, the expanded code will automatically create a private variable: NSString * name; (other classes are not accessible (including subclass )) /* @ synthesize name = _ name; this statement is expanded as follows: (memory management is not met) *-(void) setName :( NSString *) name * {* _ name = name; *} *-(NSString *) name * {* return _ name; *} */@ end
In Xcode 4.5, only the keyword @ property is used:

# Import <Foundation/Foundation. h> // Xcode 4.5 or above @ interface Person: NSObject @ property NSString * name;/** @ property NSString * name; this statement is available in @ interface: * {* NSString * _ name; *} *-(void) setName :( NSString *) name; *-(NSString *) name; ** @ property NSString * name; this statement is available in @ implementation: **-(void) setName :( NSString *) name * {* _ name = name; *} *-(NSString *) name * {* return _ name; *} */@ end @ implementation Person // Xcode4.5 no need to write @ synthesize @ end
@ Propert related parameters 1. @ What parameters does property have?

Group 1: retain assign copy strong weak unsafe_unretained autoreleasing

Group 2: readwrite readonly

Group 3: nonatomic atomic

Group 4: setter getter

2. What are the functions of parameters? ① Parameter description

The first group (retain assign copy strong weak unsafe_unretained autoreleasing) is used for: set Method Memory Management.

Assign (default parameter): generate the set method for direct assignment (excluding memory management), applicable to non-OC objects (basic data type, composite data type)

Retain: generate the set method that complies with the memory management (old value of release, new value of retain), applicable to member variables of OC objects

Copy: generate the set method that complies with the memory management (the old value of release and the new value of copy), applicable to NSString, NSArray and other immutable objects.

Strong: a Strong reference determines the survival of an object. (If an object does not have a Strong pointer (the reference counter is 0), the object will be destroyed and the memory will be released.) It points to an object, this object performs a retain operation. Applies to OC objects

Weak: a weak reference. It does not matter whether it exists or not (the weak Pointer Points to the object or not (the object reference calculator remains unchanged). It is applicable to OC objects.

Unsafe_unretained

Note:

Weak and Strong are generally used when the ARC mechanism is enabled.

Strong: a Strong reference determines the survival of an object. (If an object does not have a Strong pointer (the reference counter is 0), the object will be destroyed and the memory will be released.) It points to an object, this object performs a retain operation.

A non-ARC retain is equivalent to the strong of the ARC, and a weak reference is equivalent to assign.

The copy parameter is the same as the set method generated by using the retain parameter (replace retain in the generated set method with copy)


The second group (readwrite readonly) is used to: whether to generate the set Method

Readwrite (default parameter): both the declaration and implementation of the set and get methods are generated.

Readonly: only generate get method declaration and implementation (do not generate the declaration and implementation of the set Method)


The third group (nonatomic atomic) is used for multi-thread management.

Atomic (default parameter): atomicity and low performance (generally, apps in the Development OC are not recommended and used for high security such as finance)

Nonatomic: Non-atomic, high performance (highly recommended, high performance)

Atomic: (atomic operation) An operation execution process cannot be interrupted. If the operation is completed, it cannot be executed (an operation cannot be paused by the cpu and then scheduled ). If an operation is atomic, in a multi-threaded environment, there will be no strange problems such as variable modification (ensuring data synchronization ). Atomic operations are non-exclusive operations. suboperations are a very important concept in multi-threaded programs. They are often used to implement some synchronization mechanisms and are also the source of some common multithreading bugs.

Nonatomic: (non-atomic operation) the operation is to take a value directly from the memory (regardless of whether it is occupied), because it acquires data from the memory, it does not have a lock protection to calculate the Value in the registers in the cpu. It is used simply from the memory address and the data results stored in the current memory. High performance can be improved in multi-threaded environments, but data synchronization cannot be guaranteed.


The fourth group (setter getter) is used to rename the set and get methods (often used in the get method of the member variables of the BOOL type. The BOOL method often starts with is (the set Method is rarely used ))

Setter: Rename the set Method of the member variable. The default name of the set method is-(void) set member variable name (the first letter of the member variable name is capitalized): (member variable data type) member variable name
Getter: Rename the set Method of the member variable. The get method is named by default:-(member variable data type) member variable name ② code proof

Parameter example: 

Main. m file

/** Group 1: retain assign copy strong weak * Group 2: readwrite readonly * Group 3: nonatomic atomic * Group 4: setter getter */# import <Foundation/Foundation. h> # import "Student. h "# import" Book. h "int main (int argc, const char * argv []) {Student * stu = [[Student alloc] init]; Book * B = [[Book alloc] init]; book * b1 = [[Book alloc] init]; /******* verify retain and assign ********/NSLog (@ "B = % ld", [B retainCount]); NSLog (@ "b1 = % ld", [b1 retainCount]); stu. book = B; stu. book1 = b1; NSLog (@ "book (parameter: retain) after use B = % ld", [B retainCount]); NSLog (@ "book1 (parameter: assign) after use b1 = % ld ", [b1 retainCount]); /******************************* // stu. age = 10; an error is returned when the setAge method is called. If the setAge method does not exist, the setAge method is not generated. // The value of age in stu can be output, the get method NSLog (@ "Student age is % I", stu. age ); /*********** verify setter and getter ******************* // isMan of stu can be called. method, we didn't write the setIsMan method, which indicates that the stu is successfully renamed. isMan = YES; NSLog (@ "% d", stu. isMan); [b1 release]; [B release]; [stu release]; return 0 ;}


Student. h file

/* Verification without ARC: * First group: retain assign copy strong weak * Second group: readwrite readonly * Third group: nonatomic atomic * Fourth group: setter getter */# import <Foundation/Foundation. h> @ class Book; @ interface Student: NSObject/**********************/@ property (nonatomic, retain, readwrite) book * book;/* The above uses the retain parameter. The expanded set implementation method is: *-(void) setBook :( Book *) book * {* if (_ book! = Book) * {* [_ book release]; * _ book = [book retain]; *} ** // ************* verify assign **************/@ property (nonatomic, assign, readwrite) Book * book1;/* The above uses the retain parameter, and the expanded set implementation method is: *-(void) setBook :( Book *) book * {* _ book = book; *} ** // ************ verify readonly **************/@ property (nonatomic, assign, readonly) int age;/* the preceding readonly parameter is used to generate only the get method: *-(int) age; * {* return _ age; *} ** // *********** verify the setter and getter **************/@ property (nonatomic, assign, readwrite, setter = setIsMan:, getter = isMan) BOOL man;/* the setter parameter is used above. Only the set method name is generated and changed to isMan *-(void) isMan :( BOOL) man; (default method name: man) * {* _ man = man; *} ** use the getter parameter above. Only the get method name is generated and changed: isMan *-(BOOL) isMan; (default method name: man) * {* return _ man; *} **/@ end

Student. m file

# Import "Student. h" # import "Book. h" @ implementation Student-(void) dealloc {[_ book release]; NSLog (@ "Student is destroyed !! "); [Super dealloc] ;}@ end

Book. h file

#import <Foundation/Foundation.h>@interface Book : NSObject@property (nonatomic,copy,readwrite) NSString *name;@end

Book. m file

# Import "Book. h" @ implementation Book-(void) dealloc {[_ name release]; NSLog (@ "book destroyed !! "); [Super dealloc] ;}@ end

Parameter result output: 

12:02:53. 108 07-@ property and its parameters [2364: 221860] B = 12015-05-01 12:02:53. 109 07-@ property and its parameters [2364: 221860] b1 = 12015-05-01 12:02:53. 109 07-@ property and its parameters [2364: 221860] book (parameter: retain) after use B = 22015-05-01 12:02:53. 110 07-@ property and its parameters [2364: 221860] book1 (parameter: assign) after use b1 = 12015-05-01 12:02:53. 110 07-@ property and its parameters [2364: 221860] Student age is 02015-05-01 12:02:53. 110 07-@ property and its parameters [2364: 221860] 12 015-05-01 12:02:53. 110 07-@ property and its parameter [2364: 221860] book are destroyed !! 12:02:53. 110 07-@ property and its parameter [2364: 221860] book was destroyed !! 12:02:53. 110 07-@ property and its parameter [2364: 221860] Student was destroyed !!

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.