在 Objective-C 中比较 2 个字符串

发布于 2024-11-09 13:08:21 字数 1533 浏览 0 评论 0原文

我编写了以下代码:

   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 并在另一个方法中使用了它。我不能在这个方法中使用in吗? :/

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

I wrote the following code:

   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 is an NSArray declared in .h, I have initilase and used it in another method. I can't use in in this method? :/

But I have a crash. I don't really know where the problem is but I think it's when I compare if(date==choosedDate)? Help please

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

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

发布评论

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

评论(3

尤怨 2024-11-16 13:08:21

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

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

if([date isEqualToString:choosedDate])

When you use an == on pointers like NSString * it is comparing memory addresses, not comparing the value of strings.

The following will actually compare the string values:

if([date isEqualToString:choosedDate])
白云不回头 2024-11-16 13:08:21

在 Objective C 中比较两个字符串的更好方法是:

NSString *string1 = <your string>;
NSString *string2 = <your string>;

if ([string1 caseInsensitiveCompare:string2] == NSOrderedSame) {
    //strings are same
} else {
    //strings are not same
}

In Objective C better way to compare two string is:

NSString *string1 = <your string>;
NSString *string2 = <your string>;

if ([string1 caseInsensitiveCompare:string2] == NSOrderedSame) {
    //strings are same
} else {
    //strings are not same
}
指尖微凉心微凉 2024-11-16 13:08:21

除了使用 [date isEqualToString:choosedDate] 而不是 date==choosedDate 之外,我最初的反应是确保 depSelectedIndice 和 < code>comSelectedIndice 不引用下一行中超过 deparatureDatesgoingBackDates 末尾的元素。

NSString *choosedDate =[NSString stringWithFormat:@"%@%@",[deparatureDates objectAtIndex:depSelectedIndice], [goingBackDates objectAtIndex:comSelectedIndice]];

我不知道 depPricecomPriceprice 是否分配正确,也不知道它们的类型是什么,但它们可能会给您带来问题,以及。

In addition to using [date isEqualToString:choosedDate] instead of date==choosedDate, my initial reaction would be to make sure that depSelectedIndice and comSelectedIndice do not refer to elements past the end of deparatureDates and goingBackDates in the following line.

NSString *choosedDate =[NSString stringWithFormat:@"%@%@",[deparatureDates objectAtIndex:depSelectedIndice], [goingBackDates objectAtIndex:comSelectedIndice]];

I don't whether depPrice, comPrice, and price were allocated correctly, nor what their types are, but they could be causing you problems, as well.

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