1, Introduction:
The property is a objective-c keyword, used in pairing with @synthesize , to allow the compiler to automatically generate a method declaration with the same name as the data member. @synthesize is the implementation that is used to generate the corresponding declaration method.
Syntax format for 1.1 property:
@property (parameter 1, Parameter 2) type name;
Here are the main parameters, the following three kinds:
Setter/getter Method (assign/retain/copy)
Read-write properties (readwrite/readonly)
Atomicity (nonatomic)
1.2 Three ways to use
The assign/retain/copy represents the way in which the value is assigned.
the ReadOnly keyword means that the setter will not be generated, so it cannot be used in combination with Copy/retain/assign .
The default value for Atomicity is Atomic, and the Read function is atomic.
1.2.1 Copy/reain/assign in which to select a setter to determine how the property handles this property. The NSObject object is used in this way.
1.2.2 Some special objects, such as nssstring, use copy.
The 1.2.3 assign keyword represents the setter's direct assignment, rather than copying or preserving it. For basic data types, such as Nsinteger and cgfloat, or types that you do not directly own, such as delegates.
2, how to use property1.1 no property and a comparison of property
The header file defines obj. Use in. m files
[CPP]View Plaincopy
- #import <UIKit/UIKit.h>
- @interface Viewcontroller:uiviewcontroller
- {
- NSObject *obj;
- }
- @end
[CPP]View Plaincopy
- -(void) Viewdidload
- {
- [Super Viewdidload];
- Self.obj = nil;
- }
The prompt is not available.
Plus property
[CPP]View Plaincopy
- #import <UIKit/UIKit.h>
- @interface Viewcontroller:uiviewcontroller
- {
- NSObject *obj;
- }
- @property (Nonatomic,retain) NSObject *obj;
- @end
Compile can pass, run, crash, prompt error reason: '-[viewcontroller setobj:]: Unrecognized selector sent to instance 0x6b6c480
That's all we have. Implements setter methods.
Use the @synthesize keyword to implement getter and setter.
In the. m file
[CPP]View Plaincopy
- @implementation Viewcontroller
- @synthesize obj;
- -(void) Viewdidload
- {
- [Super Viewdidload];
- Self.obj = nil;
- }
Run, the program runs normally. The setter worked.
3. Code generated by @property and @synthesize keywords
What code did the @property and @synthesize keywords generate? We can also replace these keywords by implementing getter and setter on our own.
Comment out the code that corresponds to the two keywords
. h
[CPP]View Plaincopy
- #import <UIKit/UIKit.h>
- @interface Viewcontroller:uiviewcontroller
- {
- NSObject *obj;
- }
- @property (Nonatomic,retain) NSObject *obj;
- -(nsobject*) obj;
- -(void) Setobj: (nsobject*) NEWOBJ;
- @end
. m
[CPP]View Plaincopy
- @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 again, it can start normally. A description of the getter and setter he wrote replaced the property.
4, using three kinds of parameters comparison
@property (nonatomic, retain)nsobject *obj;
@property (nonatomic, retain, ReadWrite) nsobject *obj;
ReadWrite is the default behavior, so these two lines of code are equivalent
@property (retain) nsobject *obj;
@property (Atomic, retain) NSObject *obj;
Atomic is the default behavior, so these 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 behavior, so these three rows are equivalent.
@property nsobject *obj, is that OK? No, it's a warning .
Only the underlying data types such as int can be written like this. The object must be assigned the type of the value.
@property (retain) nsobject *obj; that's fine. When to use assign, when to use retain, and later to speak.
5, retainand copy experiments.
Use copy.
. h file
[CPP]View Plaincopy
- #import <UIKit/UIKit.h>
- @interface Viewcontroller:uiviewcontroller
- {
- NSString *string;
- }
- @property (nonatomic, copy) NSString *string;
- @end
. m file
[CPP]View Plaincopy
- @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
2012-07-19 20:41:44.853 testproject1[1213:f803] str_point:0x6a8e0b0 ABCD retaincount:1
2012-07-19 20:41:44.854 testproject1[1213:f803] string_point:0x6a8e0b0 ABCD Retaincount:2
The memory address is the same, not like other text written in the same way, copied a copy of the memory, here with copy is also a shallow copy. Retain also +1
Using retain
[CPP]View Plaincopy
- #import <UIKit/UIKit.h>
- @interface Viewcontroller:uiviewcontroller
- {
- NSString *string;
- }
- @property (nonatomic, retain) NSString *string;
- @end
The printing results are:
2012-07-19 20:42:08.113 testproject1[1230:f803] str_point:0x6d3b8f0 ABCD retaincount:1
2012-07-19 20:42:08.114 testproject1[1230:f803] string_point:0x6d3b8f0 abcd Retaincount:2,
The result is the same as the copy above.
Note: After IOS5, added automatic Reference counting (ARC), iOS5 in the New keyword has strong, weak, unsafe_unretained.
Copyright statement: This article by http://blog.csdn.net/totogo2010/Original, welcome reprint share. Please respect the author Labor, reproduced when the statement and the author of the blog link, thank you!
"Turn" objective-c syntax property detailed