Can someone post a complete example of using NSNumber?

I am not even sure, for example, if I can use it as a regular variable. There seems to be no volatile version. And does this value change, the value can be changed?

+4
source share
2 answers

What do you mean by "full use of the example"? Can you provide context for the example you are looking for?

What do you understand using something as a β€œnormal” variable?

You can get NSNumber either using the numberWithXXX functions that return an object with auto-implementation, or by using the standard alloc / init :

 NSNumber* myAllocedNumber = [[NSNumber alloc] initWithFloat:0.0f]; // ...use it... [myAllocedNumber release]; NSNumber* myAutoreleasedNumber = [NSNumber numberWithFloat:0.0f]; 

You can change what the pointer points to later, but you cannot change the value:

 NSNumber* myAutoreleasedNumber = nil; myAutoreleasedNumber = [NSNumber numberWithFloat:0.0f]; // ...use it... myAutoreleasedNumber = [NSNumber numberWithInt:1000]; 
+6
source

You can always assign an immutable variable to another object reference for this to work:

  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 } 
0
source

All Articles