objective-c - 比较 objective-C 中的 2 个字符串

我写了下面的代码:

   if (depSelectedIndice > -1 && comSelectedIndice> -1)
        {
            NSLog(@"depart elemet : %d ",depSelectedIndice);
            NSLog(@"depart elemet : %d ",comSelectedIndice);
            NSLog(@"ok1");
            NSString *choosedDate =[NSString stringWithFormat:@"%@%@",[deparatureDates objectAtIndex:depSelecif (depSelectedIndice > -1 && comSelectedIndice> -1)
    {
        NSLog(@"depart elemet : %d ",depSelectedIndice);
        NSLog(@"depart elemet : %d ",comSelectedIndice);
        NSLog(@"ok1");
        NSString *choosedDate =[NSString stringWithFormat:@"%@%@",[deparatureDates objectAtIndex:depSelectedIndice], [goingBackDates objectAtIndex:comSelectedIndice]];
        NSLog(@"0000000000001");

        NSLog(@" number of element : %d", [allCombinations count]);
//        for (int j=0; j<[allCombinations count]; j++)
//        {
//            NSLog(@"111111111111111111");
//           // NSString *date = [[allCombinations objectAtIndex:j] objectForKey:@"keydate"];
//            NSLog(@"22222222222222222222");
//              if([date isEqualToString:choosedDate])
//              {
//                  depPrice.text=@"1";
//                  comPrice.text=@"1";
//                  price.text=@"3";
//                  
//              }
       // }
    }

allCombinations 是在 .h 中声明的 NSArray,我有 initilase 并在另一个方法中使用它。我不能在这种方法中使用? :/

但是我遇到了一个崩溃。我真的不知道问题出在哪里,但我认为是在比较 if(date==choosedDate)?请帮忙

最佳答案

当你在像 NSString * 这样的指针上使用 == 时,它是在比较内存地址,而不是比较字符串的值。

下面将实际比较字符串值:

if([date isEqualToString:choosedDate])

https://stackoverflow.com/questions/6118574/

相关文章:

php - 无法移 Action 曲家

php - 如何将键添加到php数组?

ios - Firebase 未正确初始化

java - 为什么我在 Java 中对 char 和 int 进行比较不起作用?

vb.net - 如何拆分逗号分隔的字符串?

python - 如何将 __repr__ 与多个参数一起使用?

r - 如何在 R 中计算一个月的第三个星期五

.net - string 是不可变的,而 stringbuilder 是可变的

xml - 任何想法为什么图案化的 SVG 文件在浏览器中显示为空白?

angular - mat-sort-header/mat-sort 不适用于特定列