Reputation: 1666
I don't understand this exactly.
if I write:
NSNumber *number = [NSNumber initWithInteger: 5];
do I have to release it?
Is this the same as: NSNumber *number= [NSNumber alloc];
?
Upvotes: 1
Views: 200
Reputation: 111
"The general rule of thumb is: If you alloc, copy, or new, memory management's on you."
I'd add retain to this as well.
Upvotes: 2
Reputation: 51374
First of all, there is no class method named "+(id)initWithInteger:" in NSNumber class. That should be "+(id)numberWithInteger:"
You should not release it. Constructors like "[NSNumber numberWith..." are called convenient constructors. They return autoreleased objects. So, you don't have to worry about releasing them.
Upvotes: 0
Reputation: 2183
You wouldn't call NSNumber *number = [NSNumber initWithInteger: 5];
because NSNumber
does not respond to + initWithInteger:
. You actually need to call both +alloc
and -initWithInteger:
, like this:
NSNumber *number = [[NSNumber alloc] initWithInteger:5];
Then you will have to release it later; for every alloc call you make, you should also make a call to release.
Alternatively, you could get an autoreleased NSNumber
using the following class method:
NSNumber *number = [NSNumber numberWithInteger:5];
Methods like this return an autoreleased object, which means that you do not need to release it yourself later.
Upvotes: 5