I'm not even sure, for example, if I can use it like a normal variable. There doesn't appear to be a mutable version. And does mutable mean it's value can be changed?
A:
You can always assign to an immutable variable a different object reference so this works:
NSNumber *intKey;
NSMutableArray *backToString =[[NSMutableArray alloc] init];
while ((intKey = [enumerator nextObject])) {
//(NSNumber *)integer = [key integerValue];
[backToString addObject:[intKey stringValue]];
// code that uses the returned key
}
ennuikiller
2009-09-03 23:31:10
+1
A:
What do you mean by "full example usage"? Can you give a context for the kind of example you're looking for?
What do you mean by using something as a "normal" variable?
You can get an NSNumber
either by using the numberWithXXX
functions, which returns an autoreleased object, or by doing the standard alloc
/init
:
NSNumber* myAllocedNumber = [[NSNumber alloc] initWithFloat:0.0f];
// ...use it...
[myAllocedNumber release];
NSNumber* myAutoreleasedNumber = [NSNumber numberWithFloat:0.0f];
You can later change where your pointer is pointing to, but you can't change the value:
NSNumber* myAutoreleasedNumber = nil;
myAutoreleasedNumber = [NSNumber numberWithFloat:0.0f];
// ...use it...
myAutoreleasedNumber = [NSNumber numberWithInt:1000];
Shaggy Frog
2009-09-03 23:35:09
So... I can't change the value per se. But I can just assign a different nsnumber to it. Neat. Ok. It's making sense. You pretty much answered it. I have to use the release method when I'm done with it?
Neo42
2009-09-04 00:16:58
Only use release if you've used alloc, or when you've retained an object. In my above example, you might use retain on myAutoreleasedNumber if you need to keep it around for a while.
Shaggy Frog
2009-09-04 00:45:58