objective-c 如何创建浮点值的 NSMutableArray

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/493817/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:06:27  来源:igfitidea点击:

How to create an NSMutableArray of floating point values

iphoneobjective-carraysdouble

提问by Jon

I'm new to Objective-C and iPhone development, and I'm trying to store floating-point values in an NSMutableArray, but when I do I get an error saying "incompatible type for argument 1 of 'addObject". What am I doing wrong? I'm trying to create an array of doubles that I can perform math calculations with.

我是 Objective-C 和 iPhone 开发的新手,我试图在 NSMutableArray 中存储浮点值,但是当我这样做时,我收到一条错误消息,提示“'addObject 的参数 1 的类型不兼容”。我究竟做错了什么?我正在尝试创建一个可以进行数学计算的双精度数组。

回答by Ryan Townshend

NSMutableArray only holds objects, so you want an array to be loaded with NSNumber objects. Create each NSNumber to hold your double then add it to your array. Perhaps something like this.

NSMutableArray 只保存对象,因此您希望使用 NSNumber 对象加载数组。创建每个 NSNumber 来保存您的双精度值,然后将其添加到您的数组中。也许是这样的。

NSMutableArray *array = [[NSMutableArray alloc] init];
NSNumber *num = [NSNumber numberWithFloat:10.0f];
[array addObject:num];

Repeat as needed.

根据需要重复。

回答by Sijmen Mulder

Use an NSNumber to wrap your float, because the dictionary needs an object:

使用 NSNumber 来包装您的浮点数,因为字典需要一个对象:

[myDictionary setObject:[NSNumber numberWithFloat:0.2f] forKey:@"theFloat"];
/* or */
[myDictionary setObject:@0.2f forKey:@"theFloat"];

retrieve it by sending floatValue:

通过发送 floatValue 来检索它:

float theFloat = [[myDictionary objectForKey:@"theFloat"] floatValue];

Code is untested.

代码未经测试。

You can wrap many other data types in NSNumber too, check the documentation. There's also NSValuefor some structures like NSPoint and NSRect.

您也可以在 NSNumber 中包装许多其他数据类型,请查看文档。对于一些结构,如 NSPoint 和 NSRect,还有NSValue

回答by Jason Coco

In Cocoa, the NSMutableDictionary (and all the collections, really) require objects as values, so you can't simply pass any other data type. As both sjmulder and Ryan suggested, you can wrap your scalar values in instances of NSNumber (for number) and NSValue for other objects.

在 Cocoa 中,NSMutableDictionary(和所有集合,真的)需要对象作为值,所以你不能简单地传递任何其他数据类型。正如 sjmulder 和 Ryan 建议的那样,您可以将标量值包装在 NSNumber(用于数字)和 NSValue 的实例中以用于其他对象。

If you're representing a decimal number, for something like a price, I would suggest also looking at and using NSDecimalNumber. You can then avoid floating point inaccuracy issues, and you can generally use and store the "value" as an NSDecimalNumberinstead of representing it with a primitive in code.

如果您表示一个十进制数,例如价格,我建议您也查看并使用NSDecimalNumber。然后,您可以避免浮点不准确问题,并且您通常可以使用和存储“值”作为NSDecimalNumber,而不是在代码中用原语表示它。

For example:

例如:

// somewhere
NSDecimalNumber* price = [[NSDecimalNumber decimalNumberWithString:@"3.50"] retain];
NSMutableArray*  prices= [[NSMutableArray array] retain];

// ...

[prices addObject:price];

回答by Sanjay Kakadiya

NSMutableArray *muArray = [[NSMutableArray alloc] init];
NSNumber *float = [NSNumber numberWithFloat:210.0f];
NSNumber *float1 = [NSNumber numberWithFloat:211.0f];
[muArray addObject:float];
[muArray addObject:float1];

NSlog(@"my array is--%@",muArray);