获取数值而不出错“下标需要接口大小'NSArray',这在非稳定ABI中不是常数”

时间:2012-02-26 19:27:13

标签: objective-c ios cocoa-touch nsarray

如果我有一个NSArray,并且我想获得其中的每个数字,是否有一种简单的方法可以做到这一点?

我试过了这个建议 http://code.flamingleaf.com/category/objective-c/ 而不是(在其他编程语言中用于数组中下标的方法):

  

的someArray [I]

使用

  

[someArray objectAtIndex:i];

有没有办法获取数值?

我一直收到类型错误: “下标需要接口'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建议的解决方案。

1 个答案:

答案 0 :(得分:1)

如果没有看到对象是什么类,很难分辨,但如果它们是NSStringNSNumber类型,您可以使用以下任何方法来获取数值:

[[someArray objectAtIndex:i] floatValue];
[[someArray objectAtIndex:i] doubleValue];
[[someArray objectAtIndex:i] intValue];
[[someArray objectAtIndex:i] unsignedIntValue];

等...