问题描述
在在Objective-C 2.0中编程的第326页,作者说:
myNumber = [[NSNumber alloc] initWithInt:1000];
[myNumber release];
我的问题是:
如果我用这个语句创建一个 NSNumber
对象
NSNumber * myNumber = [NSNumber numberWithInteger:100];
我不必释放对象 myNumber
如果是 [NSNumber numberWithInt:]
它返回一个自动释放的对象,你不需要做任何事情正确释放。除非你保留
它,当然,在这种情况下你会调用 release
,可能从你的 dealloc
方法。
[[NSNumber alloc] initWithInt:]
一个保留计数为1的对象(从调用 alloc
)。您负责释放以这种方式创建的任何对象。
On page 326 in the book Programming in Objective-C 2.0 the author says:
myNumber = [[NSNumber alloc] initWithInt: 1000];
[myNumber release];
My question is:
Does this mean that if I create an NSNumber
object with this statement
NSNumber *myNumber = [NSNumber numberWithInteger: 100];
I don't have to release the object myNumber
myself?
In the case of [NSNumber numberWithInt:]
it returns an autoreleased object, and you don't need to do anything to release it properly. Unless you retain
it, of course, in which case you would call release
on it, likely from your dealloc
method.
[[NSNumber alloc] initWithInt:]
returns an object with a retain count of one (from calling alloc
). You are responsible for releasing any object created this way.
这篇关于释放使用initWithInteger创建的NSNumber实例:vs numberWithInt:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!