ifステートメントが実行されないのはなぜですか?
-
22-07-2019 - |
質問
次の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")
アラートを入力し、Valueが設定され、Tobyに設定されていますが、Ifステートメントはelse部分だけを実行していません。私が見逃しているのは明らかにシンプルなものです。
Objective-Cを学習しています
解決
この if
ステートメント:
if (aName == @"Toby")
ポインタではなく、文字列を比較します。あなたが欲しい:
if ([aName isEqualToString:@"Toby"])
これは単純なCと実際には違いはありません。 ==
を使用して文字列を比較することもできません。
所属していません StackOverflow