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.
精彩评论