2009-10-30 6 views
0

J'ai l'Objective-c suivant FonctionPourquoi mon instruction if ne se déclenche-t-elle pas?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


NSUInteger section = [indexPath section]; 
NSUInteger row = [indexPath row]; 

NSString *key = [keys objectAtIndex:section]; 
NSArray *nameSection = [mysearchdata objectForKey:key]; 

static NSString *SectionsTableID = @"SectionsTableID"; 
static NSString *TobyCellID = @"TobyCellID"; 

NSString *aName = [nameSection objectAtIndex:row]; 

if (aName == @"Toby") 
{ 
    TobyCell *cell = [tableView dequeueReusableCellWithIdentifier:TobyCellID];  
    if (cell == nil) 
    {  
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TobyCell" owner:self options:nil]; 
     for (id oneObject in nib) 
      if ([oneObject isKindOfClass:[TobyCell class]]) 
       cell = (TobyCell *)oneObject; 
    }          
    cell.lblName.text = [nameSection objectAtIndex:row]; 
    return cell;   
} 
else 
{ 
//standard cell loading code 
} 
} 

Tout ce que je veux est pour l'instruction if au feu lorsque la ligne est égale à mon nom - très excitant.

if (aName == @"Toby") 

J'ai mis dans une alerte et la valeur est réglée, son être mis à Toby, mais l'instruction If n'exécute pas seulement la partie else. C'est évidemment quelque chose de simple que j'ai manqué.

J'apprends Objective-C

Répondre

9

Cette déclaration if:

if (aName == @"Toby") 

compare les pointeurs, et non des chaînes. Vous voulez:

if ([aName isEqualToString:@"Toby"]) 

Ceci n'est pas vraiment différent de C simple; vous ne pouvez pas utiliser == pour comparer des chaînes là non plus.

+0

Merci. Si proche et pourtant si loin :) –