开发者

NSNumber : numberWith n initWith

Is there an开发者_运维问答y difference

[NSNumber numberWithInteger: ]

[NSNumber initWithIneger: ] ?


The number returned by +numberWithInteger: is autoreleased; the one returned by -initWithInteger: isn't. (Also, you need to +alloc NSNumber before sending -initWithInteger:, while you don't do that with +numberWithInteger:'.)

NSNumber *foo = [[NSNumber alloc] initWithInteger:7]; // you must release foo when done
NSNumber *bar = [NSNumber numberWithInteger:7];       // bar is autoreleased


initWithIneger: starts with "init", so you need to release the object this method returns.

numberWithInteger: does not start with "init" or "new" or "alloc", so the object this returns is autoreleased. You don't need to release it. If you wanna keep it for later use, you need to retain it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜