xcode 有人可以发布完整的 NSNumber 用法示例吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1376482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can someone post a full example usage of NSNumber?
提问by Neo42
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?
例如,我什至不确定我是否可以像普通变量一样使用它。似乎没有可变版本。mutable 是否意味着它的值可以改变?
回答by Shaggy Frog
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
通过使用numberWithXXX
返回自动释放对象的函数或通过执行标准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];
回答by ennuikiller
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
}