在 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 并在另一个方法中使用了它。我不能在这个方法中使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您在
NSString *
等指针上使用==
时,它会比较内存地址,而不是比较字符串的值。下面将实际比较字符串值:
When you use an
==
on pointers likeNSString *
it is comparing memory addresses, not comparing the value of strings.The following will actually compare the string values:
在 Objective C 中比较两个字符串的更好方法是:
In Objective C better way to compare two string is:
除了使用
[date isEqualToString:choosedDate]
而不是date==choosedDate
之外,我最初的反应是确保depSelectedIndice
和 < code>comSelectedIndice 不引用下一行中超过deparatureDates
和goingBackDates
末尾的元素。我不知道
depPrice
、comPrice
和price
是否分配正确,也不知道它们的类型是什么,但它们可能会给您带来问题,以及。In addition to using
[date isEqualToString:choosedDate]
instead ofdate==choosedDate
, my initial reaction would be to make sure thatdepSelectedIndice
andcomSelectedIndice
do not refer to elements past the end ofdeparatureDates
andgoingBackDates
in the following line.I don't whether
depPrice
,comPrice
, andprice
were allocated correctly, nor what their types are, but they could be causing you problems, as well.