如果我有一个 NSArray
,并且我想获取其中的每个数字,是否有一种简单的方法可以做到这一点?
我尝试了来自 http://code.flamingleaf.com/category/objective-c/ 那说而不是(其他编程语言中用于数组内下标的方法):
someArray[i]
使用
[someArray objectAtIndex:i];
有什么方法可以获取数值吗?
我不断收到以下类型的错误: “下标需要接口(interface)‘NSArray’的大小,这在非稳定 ABI 中不是常量”
感谢 Kurt Revis 提出的输入真实代码的建议。 我无法重现问题。但这是我最接近的问题:
for (int i=0; i<4; i++)
NSLog(@"%i",[[temp objectAtIndex: i]length]);
for (int i=0; i<4; i++)
tempNumbers[i]=[[temp objectAtIndex:i] length];
for (int i=0; i<4; i++)
NSLog(@"The count at i=%i is %i", i, tempNumbers[i]);
它的作用是:
3
3
5
2
the value of i = 0 and tempNumbers[i]=3.000000
the value of i = 1 and tempNumbers[i]=3.000000
the value of i = 2 and tempNumbers[i]=5.000000
the value of i = 3 and tempNumbers[i]=2.000000
结论:在我可以编写简单的代码来重现问题之前,我不会堵塞这个问题区域。
第二个结论:我将尝试 edc1591 建议的解决方案。
最佳答案
如果不看对象是什么类很难说,但是如果它们是 NSString
或 NSNumber
类型,您可以使用以下任何方法来获取数值值:
[[someArray objectAtIndex:i] floatValue];
[[someArray objectAtIndex:i] doubleValue];
[[someArray objectAtIndex:i] intValue];
[[someArray objectAtIndex:i] unsignedIntValue];
等...
关于objective-c - 获取无错误的数值 "Subscript requires size of interface ' NSArray',这在非稳定 ABI 中不是常量”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9456374/