有人可以发布 NSNumber 的完整示例用法吗?

发布于 2024-08-04 08:52:36 字数 55 浏览 9 评论 0原文

例如,我什至不确定是否可以像普通变量一样使用它。似乎没有可变版本。可变是否意味着它的值可以改变?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁把谁当真 2024-08-11 08:52:36

“完整示例用法”是什么意思?您能为您正在寻找的示例提供上下文吗?

使用某些东西作为“正常”变量是什么意思?

您可以通过使用返回自动释放对象的numberWithXXX函数或通过执行标准alloc/init来获取NSNumber

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

NSNumber* myAutoreleasedNumber = [NSNumber numberWithFloat:0.0f];

您可以稍后更改指针指向的位置,但无法更改值:

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

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];
你另情深 2024-08-11 08:52:36

您始终可以为不可变变量分配不同的对象引用,这样就可以了:

 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 
        }

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 
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文