为什么我的 if 语句没有触发?
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此
if
语句:比较指针,而不是字符串。你想要:
这与普通的 C 并没有什么不同;您也不能使用
==
来比较字符串。This
if
statement:compares pointers, not strings. You want:
This isn't really different from plain C; you can't use
==
to compare strings there either.