objective-c 用浮点数初始化 NSArray?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1519115/
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
Initialize NSArray with floats?
提问by fuzzygoat
Is this a valid way to initalize an NSArray with float objects?
这是使用浮点对象初始化 NSArray 的有效方法吗?
NSArray *fatArray = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:6.9],
[NSNumber numberWithFloat:4.7],
[NSNumber numberWithFloat:6.6],
[NSNumber numberWithFloat:6.9],nil];
It works and it feels right, just wanted to make sure I was on the right track.
它有效并且感觉正确,只是想确保我走在正确的轨道上。
gary
加里
回答by zoul
As mouviciel already wrote, this is the way to do it. When I write something like this I?usually make the code shorter using a simple macro:
正如 mouviciel 已经写的那样,这就是这样做的方法。当我写这样的东西时,我通常使用一个简单的宏来缩短代码:
#define FBOX(x) [NSNumber numberWithFloat:x]
Then you can rewrite the code like this:
然后你可以像这样重写代码:
NSArray *fatArray = [NSArray arrayWithObjects:
FBOX(6.9), FBOX(4.7), FBOX(6.6), FBOX(6.9), nil];
Macros are evil, but in this case the macro is so simple I'd use it. Plus the code hurts a?bit less to read, especially if the macro definition is not far.
宏是邪恶的,但在这种情况下,宏是如此简单,我会使用它。另外,代码读起来会少一些伤害,尤其是在宏定义不远的情况下。
If you wrote a lot code like this, you could create a category with a custom initializer with variable number of floatarguments, but then there's a problem ending the argument list. You could pass the total number of floats first:
如果你写了很多这样的代码,你可以创建一个带有可变数量float参数的自定义初始值设定项的类别,但是结束参数列表时会出现问题。您可以先传递浮点数的总数:
- (id) initWithFloats: (int) numFloats data: (float) float1, ...;
But counting the arguments by hand is prone to error. Or you could use some sentinel value such as zero that would mark the end of the argument list, but this opens a whole new can of worms called floating-point comparison.
但是手工计算参数很容易出错。或者您可以使用一些标记值(例如零)来标记参数列表的末尾,但这会打开一个全新的蠕虫罐,称为浮点比较。
Please note that nowadays you can simply write the following:
请注意,现在您可以简单地编写以下内容:
NSArray *list = @[@6.9, @4.7, @6.6, @6.9];
It's not a syntax dream come true, but it's officially supported by the compiler and it's much better than the previous solutions. See the documentation for more goodness.
这不是一个语法梦想成真,但它得到了编译器的官方支持,并且比以前的解决方案要好得多。有关更多优点,请参阅文档。
回答by mouviciel
As far as I know, this is a perfectly valid way of putting floats in an NSArray.
据我所知,这是将浮点数放入 NSArray 的一种完全有效的方法。
回答by pxl
going off on a slight tangent, if all you want to do is store an array of floats and don't really need the added functionality of an NSArray or NSNumber, then you should also consider just a standard C array:
稍微切线一点,如果您只想存储一个浮点数数组并且并不真正需要 NSArray 或 NSNumber 的附加功能,那么您还应该只考虑一个标准的 C 数组:
float fatArray[] = {6.6, 6.9, 4.7, 6.9};
回答by Mark Bessey
If you're going to be creating a lot of these arrays with exactly the same number of elements, you could define a FloatArrayFactory class that implements something like:
如果您要创建许多具有完全相同元素数量的数组,您可以定义一个 FloatArrayFactory 类来实现以下内容:
+ (NSArray *) arrayWithFloat: (float)f;
+ (NSArray *) arrayWithFloat: (float)f1 and: (float)f2;
+ (NSArray *) arrayWithFloat: (float)f1 and: (float)f2 and: (float)f3;
+ (NSArray *) arrayWithFloat: (float)f1 and: (float)f2 and: (float)f3 and: (float)f4;
And if that's too verbose, you could go for the terse and funky look of unnamed parameters:
如果这太冗长,您可以使用未命名参数的简洁和时髦的外观:
+ (NSArray *) arr: (float)f1 : (float)f2 : (float)f3;
which you can call like this:
你可以这样调用:
NSArray *a = [Factory arr: 1.0 :2.0 :3.0 :4.0];
回答by user206499
If you have a lot of floats to convert, or want to paste them in from a comma separated list, it may be easier to call a function that converts floats into an NSArray instance.
如果您有很多浮点数要转换,或者想从逗号分隔的列表中粘贴它们,调用将浮点数转换为 NSArray 实例的函数可能会更容易。
NSArray* arrayFromFloats(NSUInteger count, float *floats)
{
NSMutableArray* ma = [NSMutableArray arrayWithCapacity:count];
NSUInteger i;
for (i=0; i<count; i++) {
[ma addObject:[NSNumber numberWithFloat:floats[i]]];
}
return (NSArray*)ma;
}
Example caller:
示例调用者:
static float floats[] = {1.1, 2.2, 3.3};
NSUInteger count = sizeof(floats)/sizeof(float);
NSArray* a = arrayFromFloats(count, floats);
回答by isaac
Another lazy option is to just use an array of NSStrings and then getting the type of value you are looking for directly from the string.
另一个懒惰的选择是只使用 NSStrings 数组,然后直接从字符串中获取您要查找的值的类型。
Like so:
像这样:
NSArray *arrayOfNumbers = [NSArray arrayWithObjects:@"3.12",@"3.2", @"12", @"15", nil];
float number = [[arrayOfNumbers objectAtIndex:0] floatValue];
回答by TalkingCode
I agree with mouviciel. Since you can't put simple types into an Array this is the way to do it. Looks a bit strange and is a lot of code to write but it is fine.
我同意 mouviciel。由于您不能将简单类型放入 Array 中,这就是这样做的方法。看起来有点奇怪,要写很多代码,但很好。

