NSMutableArray 空数组错误
我正在 for 循环中使用 nsmutablearray 的数据进行一些操作。但有时它会起作用,有时它会在这一行给我一个类似“数组索引 3 空数组”的错误:
else if (min>[[enyakinarray objectAtIndex:i] floatValue])
完整代码:
for (int i=0; i<[ws3.CustomerID count]; i++) {
//radian hesaplaması
float total = [first floatValue];
float theta = total * M_PI/180;
float total2 = [second floatValue];
float theta2 = total2 * M_PI/180;
float total3 = [[ws3.Latitude objectAtIndex: i] floatValue];
float theta3 = total3 * M_PI/180;
float total4 = [[ws3.Longitude objectAtIndex: i] floatValue];
float theta4 = total4 * M_PI/180;
distance = 6371 * acos(cos(theta) * cos(theta3)
* cos(theta4 - theta2)
+ sin(theta) * sin( theta3)) ;
NSLog(@"xxxxx %f",distance);
num = [NSNumber numberWithFloat:distance];
[enyakinarray addObject:num];
NSLog(@"asasasas %@",enyakinarray);
}
float min;
NSString *s;
for (int i=0; i<[enyakinarray count]; i++) {
if(i==0)
{
min = [[enyakinarray objectAtIndex:i] floatValue];
s= [ws3.CustomerName objectAtIndex:i];
}
else if (min>[[enyakinarray objectAtIndex:i] floatValue])
{
min= [[enyakinarray objectAtIndex:i] floatValue];
s = [ws3.CustomerName objectAtIndex:i];
}
enyakinfirma.text=s;
}
我该如何解决这个问题?
I am doing some operations in for loop with nsmutablearray' s data. But sometimes it work sometimes it gives me an error like 'array index 3 empty array' at this line :
else if (min>[[enyakinarray objectAtIndex:i] floatValue])
full code :
for (int i=0; i<[ws3.CustomerID count]; i++) {
//radian hesaplaması
float total = [first floatValue];
float theta = total * M_PI/180;
float total2 = [second floatValue];
float theta2 = total2 * M_PI/180;
float total3 = [[ws3.Latitude objectAtIndex: i] floatValue];
float theta3 = total3 * M_PI/180;
float total4 = [[ws3.Longitude objectAtIndex: i] floatValue];
float theta4 = total4 * M_PI/180;
distance = 6371 * acos(cos(theta) * cos(theta3)
* cos(theta4 - theta2)
+ sin(theta) * sin( theta3)) ;
NSLog(@"xxxxx %f",distance);
num = [NSNumber numberWithFloat:distance];
[enyakinarray addObject:num];
NSLog(@"asasasas %@",enyakinarray);
}
float min;
NSString *s;
for (int i=0; i<[enyakinarray count]; i++) {
if(i==0)
{
min = [[enyakinarray objectAtIndex:i] floatValue];
s= [ws3.CustomerName objectAtIndex:i];
}
else if (min>[[enyakinarray objectAtIndex:i] floatValue])
{
min= [[enyakinarray objectAtIndex:i] floatValue];
s = [ws3.CustomerName objectAtIndex:i];
}
enyakinfirma.text=s;
}
How can I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很奇怪,但应该很容易调试。只要在
for
之前写下,您就会很快看到问题。也许您正在创建和使用之间的某个地方更改数组内容?
然而,我发现那里存在设计问题。您的代码使用多个单独的数组
CustomerName
、CustomerID
和enyakinarray
Latitude
和Longitude
其中包含单个实体 Customer 的值(名称、id、纬度、经度和距离)。也许您可以创建一个具有这些属性的对象 Customer 并仅维护一组客户?良好的 OOP 设计可以帮助您防止此类错误:)并且您可以将两个循环合并为一个:
This is strange but should be really easy to debug. Just write
before your
for
and you should see the problem very quickly. Maybe you are changing the array contents somewhere between its creation and usage?However, I see a design problem there. Your code uses several separate arrays
CustomerName
,CustomerID
andenyakinarray
Latitude
andLongitude
which contain values of a single entity Customer (name, id, latitude, longitude and distance). Maybe you could create an object Customer with these properties and mantain only one array of customers? Good OOP design helps you prevent this type of errors :)And you can merge the two cycles into one: