2011-10-27 2 views
1

Je fais des opérations dans la boucle for avec les données de nsmutablearray. Mais parfois, ça marche parfois, il me donne une erreur comme « index de tableau 3 tableau vide » à cette ligne:Erreur de tableau vide NSMutableArray

else if (min>[[enyakinarray objectAtIndex:i] floatValue]) 

code complet:

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; 
    } 

Comment puis-je résoudre ce problème?

+1

Êtes-vous sûr que '' ws3.CustomerName' contient [count] 'enyakinarray objets? – Nekto

+0

Qu'est-ce qui ressort de l'instruction log? – jrturton

+2

'array index 3 empty array' ne ressemble pas à un vrai message d'erreur, merci de poster le message exact que vous rencontrez. – DarkDust

Répondre

1

Ceci est étrange mais devrait être vraiment facile à déboguer. Il suffit d'écrire

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

avant for et vous devriez voir le problème très rapidement. Peut-être que vous modifiez le contenu du tableau quelque part entre sa création et son utilisation?

Cependant, je vois un problème de conception là-bas. Votre code utilise plusieurs tableaux séparés CustomerName, CustomerID et enyakinarrayLatitude et Longitude qui contiennent les valeurs d'une seule entité Client (nom, identifiant, latitude, longitude et distance). Peut-être pourriez-vous créer un objet Client avec ces propriétés et conserver un seul groupe de clients? Une bonne conception OOP vous aide à prévenir ce type d'erreurs :)

Et vous pouvez fusionner les deux cycles en un seul:


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];