Can someone post a full example usage of NSNumber?
-
21-09-2019 - |
Question
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?
Solution
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];
OTHER TIPS
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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow