non-ARC 的Objective-C 要点
- 程序中使用@property的地方就创建了一个对应的instance variable
- 如一个class如person是retain的,则
-
//viewDidLoad 中设置 self.person = [Person alloc] initWithArray:sbs] //这里这样会造成内存泄漏,因为person是retain的,它在setter里面(既[self setPerson])已经计数为1了。这里面又新初始化了(alloc)一个新的,所以目前self对person的引用计数为2了。怎么办呢? 个人认为哦,只好在创建的时候多加一个autorelease,因为最后self拥有person是通过setter方法,所以要在alloc后面加一个自动释放。修改后如下: self.person =[[Person alloc] initWithArray:sbs]autorelease];
3. 行规是在“在init/dealloc中不适用setter/getter”
自己可能以后会做cocos2D,
所以property在cocos2D中的规则,高手总结,原文地址http://www.raywenderlich.com/2712/using-properties-in-objective-c-tutorial
So to avoid making unnecessary properties for everything, here’s what I like to use for Cocos2D:
- Don’t use properties at all.
- Set instance variables directly to your created sprites, etc.
- Since they’ll be children of the current layer, Cocos2D will keep the retain count up as long as they’re there.
- When you remove the object from the layer, set the instance variable to nil.
I just personally find it simpler and faster to develop this way.
Note this strategy doesn’t work if you want to keep track of an object not currently in the layer – you’ll have to use an alternative strategy there.