我正在尝试对存储在两个不同数组中的两个字符串进行字符串比较。 90%的时间它工作,但另外10%的时间没有。在这种情况下,我正在比较城镇名称。
以下是我的代码:
//setup of string comparison
NSString* string1 = [information from array 1];
NSString *string2 = [information from array 2];
//remove whitespace.
NSArray* string1sep = [string1 componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceCharacterSet]];
NSString* string1rw = [string1sep componentsJoinedByString:@""];
string1rw = [string1rw lowercaseString];
NSArray* string2sep = [string2 componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceCharacterSet]];
NSString* string2rw = [string2sep componentsJoinedByString:@""];
string2rw = [string2rw lowercaseString];
//compare strings
NSLog(@"%@ (%u): %@ (%u)", string1rw, [string1rw length], string2rw, [string2rw length]);
if([string1rw isEqualToString:string2rw]){
NSLog(@"Success");
}
我的代码包含在for循环中,用于检查数组的每个元素。输出看起来像这样
-----------------------
Identifier 1
whangarei (9): whangarei (9)
whangarei (9): kaitaia (7)
whangarei (9): kerikeri (8)
whangarei (9): paruabay (8)
whangarei (9): kamo (4)
whangarei (9): tutukaka (8)
whangarei (9): auckland (8)
whangarei (9): kaiparacoast (12)
whangarei (9): paihia (6)
whangarei (9): hikurangi (9)
whangarei (9): hokianga (8)
whangarei (9): tba (3)
failure
-----------------------
Identifier 2
kaiparacoast (12): whangarei (9)
kaiparacoast (12): kaitaia (7)
kaiparacoast (12): kerikeri (8)
kaiparacoast (12): paruabay (8)
kaiparacoast (12): kamo (4)
kaiparacoast (12): tutukaka (8)
kaiparacoast (12): auckland (8)
kaiparacoast (12): kaiparacoast (12)
Success
字符串长度相同,拼写相同,均为去大写,两者都删除了空格。为什么这个用一个字符串而不是另一个?
对于人们的参考,这是我原来未清理的代码(有点难以阅读)
- (BOOL) addToFilteredResults: (int) ofset{
int catCount = 0;
int citCount = 0;
NSLog(@"-----------------------");
NSLog(@"%@", [[CHARTeventsFull objectAtIndex:ofset]objectForKey:@"event"]);
for(NSString *cityCheck in eventCities){
if([[citySelected objectAtIndex:citCount] isEqual:[NSNumber numberWithBool:YES]]){
//setup of string comparison
NSArray* cityChecksep = [cityCheck componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceCharacterSet]];
NSString* cityCheckrw = [cityChecksep componentsJoinedByString:@""];
cityCheckrw = [cityCheckrw lowercaseString];
NSString *city = [[CHARTeventsFull objectAtIndex:ofset]objectForKey:@"town"];
NSArray* citysep = [city componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceCharacterSet]];
NSString* cityrw = [citysep componentsJoinedByString:@""];
cityrw = [cityrw lowercaseString];
//comparison
NSLog(@"%@ (%u): %@ (%u)", cityrw, [cityrw length], cityCheckrw, [cityCheckrw length]);
if([cityCheckrw isEqualToString:cityrw]){
//doing another comparison here this code can be ignored for this question
//----------------------------
for(NSString *categoryCheck in eventCatagories){
if([[catagorySelected objectAtIndex:catCount] isEqual:[NSNumber numberWithBool:YES]]){
NSArray *categoriesFromEvent = [[CHARTeventsFull objectAtIndex:ofset]objectForKey:@"categories"];
for (NSString *category in categoriesFromEvent) {
NSLog(@"%@ : %@", category, categoryCheck);
if([categoryCheck isEqualToString:category]){
NSLog(@"Success");
return YES;
}
}
}
catCount++;
}
//---------------------------
}
}
citCount++;
}
NSLog(@"failure");
return NO;
}
答案 0 :(得分:0)
你检查cityCheckrw是否等于cityrw,但你不打印“成功”,除非它通过你的第二次检查(你说我们可以忽略的那个)。问题似乎更有可能是在第二次检查或第二次for循环中,而不是两个相同的字符串没有评估为相等。当您将NSLog移动到cityCheckrw / cityrw检查下时,它是否仍然不会打印“成功”?
答案 1 :(得分:0)
我已经解决了这个问题。我已经放弃使用'isEqualToString'并且现在使用谓词。不知道为什么'isEqualToString'在100%的时间内都不起作用,但在谓词中使用了基本相同的结构,并且它工作得很好。这是我对那些有相同问题的谓词所做的清理版本
1层解决方案
这有
arrayOfObjects which never changes
filteredArrayOfObjects which changes when filtered
filterArray1 which has the filters stored in it
filterArray1Selected which has a BOOL indicating if the filter is active
和代码
//remove all objects from the filter array
[filteredArrayOfObjects removeAllObjects];
//set up compounder
NSMutableArray *compounder = [[NSMutableArray alloc] init];
int count = 0;
//go though the filter array and compound all the filters that are active
for(NSString *Check in FilterArray1){
if([[filterArray1Selected objectAtIndex:count] isEqual:[NSNumber numberWithBool:YES]]){
[compounder addObject:[NSPredicate predicateWithFormat:@"(filterName == %@)", Check]];
}
count++;
}
//compound the filters
NSPredicate *compounded = [NSCompoundPredicate orPredicateWithSubpredicates:compounder];
//filter the array
NSMutableArray *filteredArrayOfObjects = [[NSMutableArray alloc] initWithArray: [ArrayOfObjects filteredArrayUsingPredicate:compounded]];
也有2层解决方案,但它变得混乱