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

发布于 2024-08-09 09:10:48 字数 1241 浏览 1 评论 0原文

我有以下 Objective-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 部分。这显然是我所缺少的简单的东西。

我正在学习 Objective-C

I have the following Objective-c Function

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

All I want is for the if Statement to fire when the Row is equal to My Name - very exciting.

if (aName == @"Toby")

I have put in an alert and the Value is being set and its being set to Toby but the If statement is not executing just the else part. It is obviously something simple that I'm missing.

I am learning Objective-C

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

勿忘初心 2024-08-16 09:10:48

if 语句:

if (aName == @"Toby")

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

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

这与普通的 C 并没有什么不同;您也不能使用 == 来比较字符串。

This if statement:

if (aName == @"Toby")

compares pointers, not strings. You want:

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

This isn't really different from plain C; you can't use == to compare strings there either.

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