Dark Horse Programmer--an explanation of @property and its parameters

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! ------@property 1. What is @property?

@property is an OC-specific keyword that is a compiler feature that automatically expands to the set and get methods of member variables when the compiler encounters @property

Prior to Xcode4.5, the Xcode compiler encountered @property would convert it to the member variable set, the Get method declaration, encountered @synthesize will expand into the member variable set, the Get method implementation, so XCode4.5, @ The property is paired with @synthesize. After Xcode4.5, Xcode encounters the @property keyword, which automatically transforms us into (Declaration of member variables, corresponding set, and the Declaration and implementation of the Get method).

2. How do we use @property?

Code Explanation:

We manually write the code (close 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
Before Xcode4.5, use the code written by @property and @synthesize pairing

Xcode 4.5 The following wording @interface person:nsobject@property nsstring *name;/* * @property nsstring *name; this line expands to: *{*  NSString *_name; *} * * (void) SetName: (NSString *) name; * * (NSString *) name; */@end @implementation person@synthesize name = _name;//Here you have to write =_name, otherwise the expanded code will automatically create a private variable: NSString *name; (Other classes are inaccessible (including subclasses)/* @synthesize name = _name; this line expands as follows: (expanded is not memory-managed)-* (void) SetName: (NSString *) name *{*  _name = name; *} * * (NSString *) name *{*   return _name; *} */@end
After Xcode 4.5, use only the keyword @property:

#import <foundation/foundation.h>//xcode more than 4.5 of the wording @interface person:nsobject@property nsstring *name;  /* * @property nsstring *name in @interface:  *{  *  nsstring *_name;  *} * *  (void) SetName: (NSString *) name;  * * (NSString *) name;  *  * @property nsstring *name; This sentence unfolds in the @implementation: * * *  (void) SetName: (NSString *) name  *{  *  _name = name;  *} * *  (NSString *) name  *{  *   return _name;  * * */  @end @implementation person//xcode4.5, do not write @synthesize@end
What are the parameters of the relevant parameters of the @propert [email protected]?

First group: Retain assign copy strong weak unsafe_unretained autoreleasing

Group II: ReadWrite readonly

Group III: Nonatomic Atomic

Group Fourth: Setter getter

2. What are the functions of the parameters respectively? ① parameter Description

First set (Retain assign copy strong weak unsafe_unretained autoreleasing) for: Set Method memory Management

Assign (default parameter): A set method that generates a direct assignment (regardless of memory management) for non-OC objects (base data type, composite data type)

Retain: Generating a set method that conforms to memory management (release old value, retain new value), for member variables of OC objects

Copy: Generates a memory-managed set method (release old value, copy new value) for immutable objects such as NSString, Nsarray, and so on.

Strong: A strong reference determines the survival of an object (an object that, if no strong pointer is pointed to (a reference counter of 0), is destroyed, freeing memory), which points to an object, which is the equivalent of an object doing a retain operation. Applies to OC objects

Weak: Weak reference, whether its existence or not (the weak pointer to the object or not (the object reference calculator does not change)), for OC object

Unsafe_unretained: Please view: Surf Tianya's Blog

Attention:

Weak and strong are generally used under open arc mechanism

Strong: A strong reference determines the survival of an object (an object that, if no strong pointer is pointed to (a reference counter of 0), is destroyed, freeing memory), which points to an object, which is the equivalent of an object doing a retain operation.

Non-arc retain, equivalent to ARC's strong, weak reference equivalent to assign

Using the copy parameter is consistent with the Set method produced using the Retain parameter (change the retain in the generated set method to copy)


The second group (ReadWrite readonly) is used to: whether to generate a set method

ReadWrite (default parameter): the Declaration and implementation of generating set and get methods at the same time

ReadOnly: Generate only declarations and implementations of Get methods (Declaration and implementation of methods that do not generate a set)


Third group (nonatomic Atomic) for: Multithreading management

Atomic (default parameter): Atomicity, low performance (general development of OC in the app is not recommended to use, do financial and other high security when used)

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

Atomic: (Atomic operation) is an operation execution process cannot be interrupted , or just finish it . , or do not do it . ( an operation cannot be paused by a midway CPU and then dispatched ). If an operation is atomic , in a multithreaded environment , there will be no strange problems such as variable modification (guaranteed data synchronization). Atomic operations are non-split operations, and atomic manipulation is a very important concept in multi-threaded programs, which is often used to implement some synchronization mechanisms and is also the source of some common multithreaded bugs .

nonatomic: (Non-atomic operation) The operation is to take a numeric value directly from memory (regardless of whether it is occupied) because it takes data from memory, and it does not have a lock-in protection for CPU the calculation of registers in Value , it simply uses the data results from the memory address of the current memory store. Performance can be improved in a multithreaded environment, but data synchronization is not guaranteed.


Group fourth (Setter getter) for: Set, get method rename (get method commonly used for member variables of type bool, bool method often starts with is (set method is seldom used))

setter: Rename the Set method of the member variable, the set method is named by default:-(void) Set member variable name (member variable name 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

examples of parameters are demonstrated:  

MAIN.M file

/* First group: Retain assign copy strong weak * Second group: ReadWrite ReadOnly * Third group: nonatomic Atomic * Group Fourth: Setter getter */#import <Foundation/Foundation.h> #import "Student.h" #import "Book.h" int main (int argc, const char * argv[]) {Student *s    Tu = [[Student alloc]init];    Book *b = [[book Alloc]init];    Book *B1 = [[book Alloc]init];    /******* verify retain with assign********/NSLog (@ "b =%ld", [b retaincount]);    NSLog (@ "B1 =%ld", [B1 retaincount]);    Stu.book = b;    Stu.book1 = B1;    NSLog (@ "book (parameter: Retain) after using B =%ld", [b retaincount]);        NSLog (@ "Book1 (parameter: Assign) after use B1 =%ld", [B1 retaincount]); /*********** authentication readonly*****************///stu.age = 10; Call Setage method Error, Setage method does not exist, indicating that no Setage method is generated//can output the value of age in Stu,    The description generates a GET method NSLog (@ "Student is%i", stu.age);    /*********** validation Setter and getter****************///Can call Stu Isman method, and we did not write Setisman method, indicating that renaming succeeded Stu.isman = YES;            NSLog (@ "%d", Stu.isman);    [B1 release];    [B release];    [Stu release]; return 0;}


Student.h file

/* Verify under Non-arc (off 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/*********** Verification retain******* /@property (nonatomic,retain,readwrite) book *book;/* above using the retain parameter, the expanded set implementation method is: * (void) Setbook: (book *) book *{ * if (_book! = book) * {* [_book release], * _book = [book retain]; *} *}**//*********** validation assign*************/@  Property (nonatomic,assign,readwrite) book *book1;/* above using the retain parameter, the expanded set implementation method is: * (void) Setbook: (book *) book *{* _book = Book; *} **//*********** Verify readonly*************/@property (nonatomic,assign,readonly) int age;/* using the readonly parameter, generate only the Get method: *-(int) age; *{* return _age; *} **//*********** Verify setter and getter*************/@property (nonatomic,assign,readwrite,setter= Setisman:,getter=isman) BOOL man;/* above using setter parameters, only the generated set method name is modified to: Isman * (void) Isman: (BOOL) man; (the default method is named: Mans) *{* _man = m An *} * * above using getter parameter, generate only getThe method name is modified to: 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 is destroyed!!");    [Super Dealloc];} @end

parameter result output:  

2015-05-01 12:02:53.108 [email protected] and its parameters [2364:221860] b = 12015-05-01 12:02:53.109 [email protected] and its parameters [ 2364:221860] B1 = 12015-05-01 12:02:53.109 [email protected] and its parameters [2364:221860] Book (parameter: Retain) after use B = 22015-05-01 12:02:53.110 [email protected] and its parameters [2364:221860] Book1 (Parameters: Assign) after use B1 = 12015-05-01 12:02:53.110 [email protected] and its parameters [2364:221860] Student age is 02015-05-01 12:02:53.110 [email protected] and its parameters [2364:221860] 12015-05-01 12:02:53.110 [email Protected] and its parameters [2364:221860] book destroyed!!2015-05-01 12:02:53.110 [email protected] and its parameters [2364:221860] book destroyed!!2015-05-01 12:02:53.110 [email protected] and its parameters [2364:221860] student be destroyed!!

Dark Horse Programmer--an explanation of @property and its parameters

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.