比较objective-C中的两个字符串
我编写了以下代码:比较objective-C中的两个字符串,objective-c,iphone,string,nsstring,comparison,Objective C,Iphone,String,Nsstring,Comparison,我编写了以下代码: if (depSelectedIndice > -1 && comSelectedIndice> -1) { NSLog(@"depart elemet : %d ",depSelectedIndice); NSLog(@"depart elemet : %d ",comSelectedIndice); NSLog(@"ok1");
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";
//
// }
// }
}
if(depselecteddince>-1和&comselecteddince>-1)
{
NSLog(@“离开要素:%d”,已退出选择项);
NSLog(@“出发要素:%d”,ComSelectedDice);
NSLog(@“ok1”);
NSString*choosedDate=[NSString stringWithFormat:@“%@%@,[deparatureDates对象索引:depSelecif(DEPSELECTEDDICE>-1&&COMSELECTEDDICE>-1)
{
NSLog(@“离开要素:%d”,已退出选择项);
NSLog(@“出发要素:%d”,ComSelectedDice);
NSLog(@“ok1”);
NSString*choosedDate=[NSString STRINGWITH FORMAT:@“%@%@”,[Deparatates对象索引:DepSelectedDice],[goingBackDates对象索引:ComSelectedDice];
NSLog(@“0000000000001”);
NSLog(@“元素数:%d”,[allCombinations count]);
//对于(int j=0;j当您在诸如NSString*
之类的指针上使用=
时,它是在比较内存地址,而不是比较字符串的值
下面将实际比较字符串值:
if([date isEqualToString:choosedDate])
除了使用[date IsequalString:choosedDate]
而不是date==choosedDate
,我的最初反应是确保depSelectedDice
和comSelectedDice
不引用下一行中超过deparatureDates
和goingBackDates
结尾的元素
NSString *choosedDate =[NSString stringWithFormat:@"%@%@",[deparatureDates objectAtIndex:depSelectedIndice], [goingBackDates objectAtIndex:comSelectedIndice]];
我不知道deprice
、comPrice
和price
是否正确分配,也不知道它们的类型是什么,但它们也可能会给您带来问题。在目标C中比较两个字符串的更好方法是:
NSString *string1 = <your string>;
NSString *string2 = <your string>;
if ([string1 caseInsensitiveCompare:string2] == NSOrderedSame) {
//strings are same
} else {
//strings are not same
}
NSString*string1=;
NSString*string2=;
if([string1不区分大小写比较:string2]==SensorDeredName){
//字符串是相同的
}否则{
//字符串不一样
}
当崩溃发生时,您能添加堆栈跟踪吗?@dredful@dredful@Chris谢谢大家。我编辑了我的帖子。问题来自所有组合。我在控制台中有0000000000001。崩溃是在我进行[allCombinations count]时发生的。控制台中没有错误,请在此处发布:(帮助请确认)“allCombinations是一个在.h中声明的NSArray,我有initilase并在另一个方法中使用它。我不能在这个方法中使用?:/”-您必须向我们展示与allCombinations
相关的代码/方法,以便我们可以看到您是否对它做了错误的操作。