栈:局部变量,自动释放池
堆:对象,动态分配 //需管理内存
对象的基本结构:oc对象+引用计数器4个字节
对象被销毁,系统会向对象发送一条dealloc方法。
野指针、僵尸对象、retain返回对象本身。
谁retain alloc,谁release
- (void)setBook:(Book *)book {
if(_book != book) {
[_book release];
_book = [book retain];
}
}
@property (nonatomic, retain) Book *book;
- (void)dealloc {
[_book release];
[super dealloc];
}
@property 不能再实现getter方法。
@property参数:retain,assign,
copy:release旧值,copy新值
readwrite,readonly,
nonatomic,atomic(默认)
@property (getter = we,setter = setWe:) intweight;
[p setWe:20] ; //更改方法名。
[p we];
//bool型
@property(getter = isRich) bool rich;
@property (nonatomic, assign) time_t/longtime;
循环引用解决方案:一端用retain,一端用assign;
@autoreleasepool {
Person*p = [[[Person alloc] init] autorelease];
}
//autoreleasepool:适用于小型数据