1 alloc and release appear in pairs
2 Set method
2.1 Basic Type Direct Assignment
2.2 OC Object: Judge unequal, retain new, release old, and then assign value
3 Dealloc method
3.1 Be sure to call [Super Dealloc]; and put it in the last
3.2 Self-owned object to do once release
@interface Book{int _price;} -(void) Setprice: (int) price;-(int) Price; @end
@implementation book-(void) Setprice: (int) price{_price = Price;} -(int) Price{return _price;} -(void) Dealloc{[_book Release];_book = nil;[ Super Dealloc];} @end
@interface person{book* _book;} -(void) Setbook: (book*) book;-(book*) book; @end
@implementation person-(void) Setbook: (book*) book{//retain new, release old, assign new to old if (_book! = book) {[Book retain];[ _book release];_book = Book;} /* Can be abbreviated as: if (_book! = book) {[_book release];_book = [book retain];} */}-(book*) Book{return _book;} @end
#import <foundation/foundation.h>int Main () {person* person = [[Person alloc] init]; book* book = [[Book alloc] init]; [Person Setbook:book]; [Book Release];book = nil; [Person Release];return 0;}
iOS Review Note 5: Set Method of memory management