2009-12-02 5 views
0

i ai NSMutableArray qui contenant les informations d'étudiants, maintenant je veux juste extraire le nom des élèves et leur note moyenne seulement, alors ce que je l'ai faitcomment créer chaîne de tableau

NSMutableArray *stuCollection = [[NSMutableArray alloc] initWithCapacity:[students count]]; 

for (int i = 0; i < [students count]; i++) 
{ 
    if([students objectAtIndex:i] != NULL) 
    { 

    NSDictionary *dic = [students objectAtIndex:i]; 
    NSString *temp = [dic objectForKey:@"name"]; 

    [stuCollection addObject:name]; 

    } 
} 


for(int j=0; j< [stuCollection count]; j++) 
{ 
    NSLOG(@"Name %@",[stuCollection objectAtIndex:j]); 
} 

je suis en mesure d'exécuter cette pour la première fois, mais quand je fais le balayage automatique, je peux exe pour la 1ère, 2ème, 3ème boucle mais ensuite les applications terminent montrent comme ci-dessous,

2009-12-02 14: 57: 37.908 AppBuzz [13073: 207] * Terminaison de l'application en raison d'une exception non interceptée 'NSInvalidArgumentException', raison: '* - [NSCFArray insertObject: atIndex:]: tentative d'insertion nulle' 02.12.2009 14: 57: 37,916 AppBuzz [13073: 207] Stack: ( 820145437, 837578260 , 819694387, 819694291 , 814683071, 814716415 , 814716245, 17529, 24097 , 814480795, 819893443, 819891231 , 858682228, 861592624 , 861585968, 8997,) Mettre fin à appelé après avoir lancé une instance de programme 'NSException' signal reçu: « SIGABRT ».

comment cela peut être améliorer

grâce

Répondre

1

Comprenez-vous les assertions?

assert(students); 
assert([students count]); 

NSMutableArray * stuCollection = [[NSMutableArray alloc] initWithCapacity:[students count]]; 
assert(stuCollection); 

for (int i = 0; i < [students count]; i++) { 
    if ([students objectAtIndex:i] != NULL) { 

     NSDictionary * dic = [students objectAtIndex:i]; 
     assert(dic); 

     NSString * temp = [dic objectForKey:@"name"]; 
     assert(temp); 

     assert(name); 

     [stuCollection addObject:name]; 
    } 
} 
... 
0

Comme il ressemble à des étudiants est une collection Cocoa, vous pouvez utiliser les éléments suivants:

NSArray *studentNames = [students valueForKey:@"name"];

Voir la KVC Set and Array Operators pour plus d'informations .

Questions connexes