1. 《iPhone开发基础教程》P27下半页的代码,buttonPressed函数里面,title为啥不用release?
2. 何时会需要在代码中主动调用dealloc?
3. 变量都是指针?如果release引用计数到零了,那变量的值是啥?之所以这么问是因为在Learn Objective-C教程 中提到:
We can also use this to improve our dealloc method slightly:
This works because when we set nil as an instance variable, the setter just retains nil (which does nothing) and releases the old value. This approach is often better for dealloc because there's no chance of the variable pointing at random data where an object used to be.
4. 考考大家,上面这段代码,为啥非要用self.<var> 的形式,而不是直接caption = nil;呢?
5. 关于内存管理,话说实践中需要创建一个object不外两种情况:
a. To keep it as an instance variable
b. To use temporarily for single use inside a function
情况a,一般有个setter,setter会释放旧的,retain新的,所以只要在dealloc里release即可;
情况b,在函数中临时用一下,有个简单原则,if you create an object with alloc or copy , send it a release or autorelease message at the end of the function. If you create an object any other way, do nothing.
例子:
NSNumber* value1 = [[NSNumber alloc ] initWithFloat:8.75];
NSNumber* value2 = [NSNumber numberWithFloat :14.78];
// only release value1, not value2
[value1 release ];
这个原则似乎解释了上面问题1,至于其原理,暂时不要问我。