Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone post a full example usage of NSNumber?

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?

like image 768
Neo42 Avatar asked Sep 03 '09 23:09

Neo42


1 Answers

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];
like image 66
Shaggy Frog Avatar answered Sep 30 '22 04:09

Shaggy Frog