NSMutableArray 空数组错误

发布于 2024-12-11 21:24:38 字数 1423 浏览 0 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白色秋天 2024-12-18 21:24:38

这很奇怪,但应该很容易调试。只要在 for 之前写下

NSLog("First array: %@", enyakinarray)
NSLog(@"Second array: %@", ws3.CustomerName)
NSLog(@"Second array: %@", ws3.CustomerID)

,您就会很快看到问题。也许您正在创建和使用之间的某个地方更改数组内容?

然而,我发现那里存在设计问题。您的代码使用多个单独的数组 CustomerNameCustomerIDenyakinarray LatitudeLongitude 其中包含单个实体 Customer 的值(名称、id、纬度、经度和距离)。也许您可以创建一个具有这些属性的对象 Customer 并仅维护一组客户?良好的 OOP 设计可以帮助您防止此类错误:)

并且您可以将两个循环合并为一个:


float minDistance = FLT_MAX;
NSUInteger customerIndex = 0;

for (NSUInteger i = 0; i < [ws3.CustomerID count]; i++) {
    /* distance calculation code ... */

    float distance = 6371 * acos(cos(theta) * cos(theta3)
                           * cos(theta4 - theta2)
                           + sin(theta) * sin(theta3));

    if (distance < minDistance) {
        minDistance = distance;
        customerIndex = i;
    }
}

enyakinfirma.text = [ws3.CustomerName objectAtIndex:customerIndex];

This is strange but should be really easy to debug. Just write

NSLog("First array: %@", enyakinarray)
NSLog(@"Second array: %@", ws3.CustomerName)
NSLog(@"Second array: %@", ws3.CustomerID)

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 and enyakinarray Latitude and Longitude 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:


float minDistance = FLT_MAX;
NSUInteger customerIndex = 0;

for (NSUInteger i = 0; i < [ws3.CustomerID count]; i++) {
    /* distance calculation code ... */

    float distance = 6371 * acos(cos(theta) * cos(theta3)
                           * cos(theta4 - theta2)
                           + sin(theta) * sin(theta3));

    if (distance < minDistance) {
        minDistance = distance;
        customerIndex = i;
    }
}

enyakinfirma.text = [ws3.CustomerName objectAtIndex:customerIndex];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文