开发者

NSNumber : numberWith n initWith

开发者 https://www.devze.com 2023-04-01 02:36 出处:网络
Is there an开发者_运维问答y difference [NSNumber numberWithInteger:] [NSNumber initWithIneger:] ?The number returned by +numberWithInteger: is autoreleased; the one returned by -initWithInteger: is

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

精彩评论

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