I. Main Functions of retain attributes
1. O-C memory management and point syntax
1> OC Memory Management normally requires a large number of retain and relrese operations
2> point syntax can reduce the use of retain and release operations
Ii. @ property (retain) How does the compiler declare
The compiler varies with the retain extension in @ property.
It is mainly to release the last value and add this counter
Declared in dog. h:
@ Property (retain) dog * dog;
Expanded:
-(Void) setdog :( dog *) adog;
-(Dog *) dog;
Iii. How to Implement the @ synthesize Compiler
Implementation in dog. M:
@ Synthesize dog = _ dog;
Expanded:
-(Void) setdog :( dog *) adog {
If (_ Dog! = Adog ){
[_ Dog release];
_ Dog = [adog retain];
}
}
-(Dog *) dog {
Return _ dog;
}
4. dealloc content
Dealloc must release dog (retain)
In dog. m
-(Void) dealloc
{
Self. Dog = nil;
[Super dealloc];
}
V. Main Functions of the copy attribute
The copy attribute completely copies the object, and the counter is set to 1, which is completely out of the relationship with the copied data.
@ Property (copy) nsstring * STR;
// The getter function of the attribute.
-(Double) Str
{
Return STR;
}
// Setter function of the attribute
-(Void) setstr :( nsstring *) newstr
{
STR = [newstr copy];
}
Vi. Assign, retain, copy
1. Foo = value; // simple Reference Assignment
2. Foo = [value retain]; // reference the value assignment and add the counter of Value
3. Foo = [value copy]; // copy the value to foo. After copying, foo and value are irrelevant.
Code in person. h
View code
#import <Foundation/Foundation.h>#import "Dog.h"@interface Person : NSObject{ Dog *_dog;}//- (void) setDog:(Dog *)aDog;//- (Dog *) dog;@property (retain) Dog *dog;@end
Person. m
View code
# Import "person. H" @ implementation person @ synthesize dog = _ dog; //-(void) setdog :( dog *) adog // {// If (adog! = _ Dog) {// [_ Dog release]; // _ dog = [adog retain]; /// let the _ Dog technician + 1 //} //-(Dog *) Dog // {// return _ dog; //}-(void) dealloc {nslog (@ "person is dealloc"); // release the _ Dog owned by a person/[_ Dog release], _ dog = nil; self. dog = nil; // [self setdog: Nil]; [Super dealloc] ;}@ end
Dog. h
View code
#import <Foundation/Foundation.h>@interface Dog : NSObject{ int _ID;}@property int ID;@end
Dog. m
View code
#import "Dog.h"@implementation Dog@synthesize ID = _ID;- (void) dealloc{ NSLog(@"dog %d is dealloc", _ID); [super dealloc];}@end
Main. m
View code
# Import <Foundation/Foundation. h> # import "person. H "# import" dog. H "// person // dog. // person dog.int main (INT argc, const char * argv []) {@ autoreleasepool {nslog (@ "Hello, world! "); Dog * dog1 = [[DOG alloc] init]; [dog1 setid: 1]; dog * dog2 = [[DOG alloc] init]; [dog2 setid: 2]; person * Xiaoli = [[person alloc] init]; [Xiaoli setdog: dog1]; [Xiaoli setdog: dog2]; [dog1 release]; [Xiaoli release]; [dog2 release]; # If 0 dog * dog1 = [[DOG alloc] init]; [dog1 setid: 1]; person * Xiaoli = [[person alloc] init]; // Xiao Li wants to walk the dog [Xiaoli setdog: dog1]; person * Xiaowang = [[person alloc] init]; [Xiaowang setdog: dog1]; nslog (@ "dog1 retain count is % lD", [dog1 retaincount]); // dog1 retain count is 3 [dog1 release]; nslog (@ "dog1 retain count2 is % lD", [dog1 retaincount]); // dog1 retain count2 is 2 [Xiaowang release]; nslog (@ "dog1 retain count3 is % lD", [dog1 retaincount]); // person is dealloc // dog1 retain count3 is 1 [Xiaoli release]; // person is dealloc // DOG 1 is dealloc # endif} return 0 ;}