代码之家  ›  专栏  ›  技术社区  ›  Toby Allen mercator

为什么我的if语句没有被触发?

  •  0
  • Toby Allen mercator  · 技术社区  · 14 年前

    我有以下目标C功能

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

    我只想让if语句在行等于我的名字时触发—非常令人兴奋。

    if (aName == @"Toby")
    

    我已经发出了一个警报,并且该值正在被设置,它被设置为toby,但是if语句不只是执行else部分。很明显我错过了一些简单的事情。

    我正在学习目标-C

    1 回复  |  直到 14 年前
        1
  •  9
  •   Carl Norum    14 年前

    这个 if 声明:

    if (aName == @"Toby")
    

    比较指针,而不是字符串。你想要:

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

    这和普通的C没什么区别,你不能用 == 也可以在那里比较字符串。