Explanation of Objective-C syntax property

Source: Internet
Author: User
1. Introduction:

Property is a keyword of Objective-C. It is used in conjunction with @ synthesize to enable the compiler to automatically generate a method declaration with the same name as the data member. @ Synthesize is used to generate the implementation of the corresponding declaration method.

1.1 The syntax format of property:

@ Property (parameter 1, parameter 2) type name;

The following three parameters are provided:

Setter/getter method (assign/retain/copy)

Read/write attributes (readwrite/readonly)

Atomicity (nonatomic)

1.2 usage in three modes

Assign/retain/copy indicates the assignment method.

The readonly keyword indicates that the setter will not be generated, so it cannot be used in combination with copy/retain/assign.

The default value of atomicity is atomic, And the READ function is atomic.

1.2.1 copy/reain/assign select a setter in the file to determine how the property is processed. The NSObject object adopts this method.

1.2.2 some special objects, such as NSSstring, use copy.

1.2.3 The assign keyword indicates that setter directly assigns a value, instead of copying or retaining it. Applicable to basic data types, such as NSInteger and CGFloat, or types you do not directly own, such as delegates.

2. How to Use property1.1 without comparing property and property

Define obj in the header file. Use in the. m file

#import <UIKit/UIKit.h>@interface ViewController : UIViewController{    NSObject *obj;}@end
- (void)viewDidLoad{    [super viewDidLoad];        self.obj = nil;、}

The prompt is unavailable.

Add property

#import <UIKit/UIKit.h>@interface ViewController : UIViewController{    NSObject *obj;}@property (nonatomic,retain) NSObject *obj;@end

After compilation, run, and crash, the error reason: '-[ViewController setObj:]: unrecognized selector sent to instance 0x6b6c480 is displayed.

That is, we have nothing to implement the setter method.

Use the @ synthesize keyword to implement getter and setter.

In the. m file

@implementation ViewController@synthesize obj;- (void)viewDidLoad{    [super viewDidLoad];    self.obj = nil;}

Run. The program runs normally. It indicates that setter takes effect.

3. Code generated by the @ property and @ synthesize keywords

What code is generated by the @ property and @ synthesize keywords? Getter and setter can also replace these keywords.

Comment out the code corresponding to the two keywords

. H

#import <UIKit/UIKit.h>@interface ViewController : UIViewController{    NSObject *obj;}//@property (nonatomic,retain) NSObject *obj;-(NSObject*)obj;-(void)setObj:(NSObject*)newObj;@end

. M

@implementation ViewController//@synthesize obj;- (void)viewDidLoad{    [super viewDidLoad];    self.obj = nil;}-(NSObject*)obj{    return obj;}-(void)setObj:(NSObject*)newObj{    if(obj != newObj){        [obj release];        obj = [newObj retain];    }}

Run the command again and start the instance properly. It indicates that the getter and setter you write replace property.

4. Comparison of Three Parameters

@ Property (nonatomic, retain) NSObject * obj;

@ Property (nonatomic, retain, readwrite) NSObject * obj;

Readwrite is the default action, so the two lines of code are equivalent.

@ Property (retain)
NSObject * obj;

@ Property (atomic, retain) NSObject * obj;

Atomic is the default action, so the two lines of code are equivalent.

@ Property (atomic, assign) int number;

@ Property (atomic)
Int number;

@ Property int number;

For int, atomic assign is the default action, so these three rows are equivalent.

@ Property NSObject * obj; do you want to write this line? No. An alarm is reported.


Only basic data types such as int can be written in this way. The object must be assigned a value.

@ Property (retain)
NSObject * obj. When to use assign, when to use retain, and copy will be discussed later.

5. retain and copy experiments.

Use copy.

. H file

#import <UIKit/UIKit.h>@interface ViewController : UIViewController{    NSString *string;}@property  (nonatomic, copy) NSString *string;@end

. M file

@synthesize string;- (void)viewDidLoad{    [super viewDidLoad];        NSString *str = [[NSString alloc] initWithFormat:@"abcd"];    NSLog(@"str_Point:%p  %@  retainCount:%d", str, str, [str retainCount]);    self.string = str;    NSLog(@"string_Point:%p  %@  retainCount:%d", string, string, [string retainCount]);}

Print results

20:41:44. 853 TestProject1 [1213: f803] str_Point: 0x6a8e0b0 abcd retainCount: 1

20:41:44. 854 TestProject1 [1213: f803] string_Point: 0x6a8e0b0 abcd retainCount: 2

The memory address is the same. instead of copying a copy of memory as other texts, copy is also a shortest copy here. Retain + 1

Use retain

#import <UIKit/UIKit.h>@interface ViewController : UIViewController{    NSString *string;}@property  (nonatomic, retain) NSString *string;@end

The printed result is:

20:42:08. 113 TestProject1 [1230: f803] str_Point: 0x6d3b8f0 abcd retainCount: 1

20:42:08. 114 TestProject1 [1230: f803] string_Point: 0x6d3b8f0 abcd retainCount: 2,

The result is the same as the previous copy.

Note: After IOS5, the Automatic Reference Counting (ARC) is added. In iOS5, the newly added keywords include strong, weak,
Unsafe_unretained.

Copyright Disclaimer: This article will be published at http://blog.csdn.net/totogo2010/, and you will be welcomed to enjoy the transfer and sharing. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you!

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.