Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
Yet another stupid question....

NSNumber has things like initWithInt: etc., but there isn't an initWithNumber or initWithObject. Can I just do numberOne=numberTwo, or am I not seeing a crucial method?
Nate
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
NSNumber implements NSCopying, so you can call -copy to get a new NSNumber * that points to a new NSNumber instance with the same value. Don't use =, as that will just be pointer assignment, so both NSNumber *s will be pointing to the same NSNumber instance. That's probably OK since NSNumber is immutable, but you wouldn't want to have your original freed while depending on it with the new name.

I don't know the intricacies here, so someone else might want to say if = results in an increment in the reference count. I don't think so.

-Lee
 

Bakerman

macrumors member
Jan 31, 2005
62
7
Sweden
Since NSNumber is immutable you don't need to copy it, you can just retain it like so:

NSNumber *number2 = [number1 retain];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.