2013-10-03 67 views
1

比较两个NSString数组时,出现问题。 其中一个数组不断更改,因为它链接到UITextField,所以当你写任何东西时,它都存储在数组中。查找两个字符串中匹配字的序列

-(void) tick: (ccTime) dt{ 


NSString *level = (NSString *)gameData[2]; //this doesn't change. example: "one two three .." 

NSString *text = (NSString *)tf.text; //text field keeps changing as I write 

NSArray *separatedText = [text componentsSeparatedByString:@" "]; 
NSArray *separatedLevel = [level componentsSeparatedByString:@" "]; 

我要检查是否有任何您与任何被存储在级阵列中的话写匹配的话。 例如, 级别是:“比较两个字符串” 而我写“比较” 所以这个方法,会返回那1个字是匹配的。所以如果我写“比较两个”它返回2个单词匹配。

我试着用这个代码:

for (int i=0;i<separatedText.count;i++){ 
    for (int j=0;j<separatedLevel.count;j++){ 
     if (![separatedText[i] caseInsensitiveCompare:separatedLevel[j]]){ 
      NSLog(@"OK"); 

      } 

     }else{ 
      NSLog(@"NO"); 
     } 


    } 

} 

,但它不能正常工作。 任何想法?

非常感谢。

编辑

caseInsensitiveCompare不返回BOOL,但它为我工作的方式。 如果我运行此代码级别“一二三”和文字“一”的结果是:

OK 
NO 
NO 

,并以“二合一”的结果是:

NO 
OK 
NO 

当它应该是OK OK NO

EDIT 2

很抱歉,如果我表示自己错了。

我想要的结果是“OK OK NO”,当我写两个匹配的词 或者可能返回匹配数的结果。 因此,与前面的例子:

级别: “一二三” 文字: “一两个” 结果: “2个匹配的单词”

回答

1

的问题是,你需要增加i,如果你找到一个匹配,并保持您所做的顺序匹配的计数。

我已经实现了它作为一个C函数在这里,但你应该没有问题,将其转换为一个Objective-C类方法

#import <Foundation/Foundation.h> 

NSUInteger numSequentialMatches(NSString *s1, NSString *s2) { 
    NSUInteger sequence = 0, highestSequence = 0; 
    NSArray *a1 = [s1 componentsSeparatedByString:@" "]; 
    NSArray *a2 = [s2 componentsSeparatedByString:@" "]; 
    for (NSInteger i = 0; i < a1.count; i++) { 
     for (NSInteger j = 0; j < a2.count; j++) { 
      if ([a1[i] caseInsensitiveCompare:a2[j]] == NSOrderedSame) { 
       if (i < a1.count) 
        i++; 
       if (++sequence > highestSequence) 
        highestSequence = sequence; 
      } else { 
       sequence = 0; 
      } 
     } 
    } 
    return highestSequence; 
} 

int main(int argc, const char **argv) { 
    @autoreleasepool { 
     NSLog(@"%lu", numSequentialMatches(@"one two three", @"one")); 
     NSLog(@"%lu", numSequentialMatches(@"one two three", @"one two")); 
    } 
    return 0; 
} 

$ clang -o array array.m -framework Foundation 
$ ./array 
2013-10-03 15:21:08.166 array[17194:707] 1 
2013-10-03 15:21:08.167 array[17194:707] 2 
0

使用NSPredicates代替。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains '%@'",separatedText]; 

     NSArray *filteredArray = [separatedLevel filteredArrayUsingPredicate:predicate ]; 
     if ([filteredArray count] == 0){ 
      NSLog(@"No elements"); 
     } 
     else{ 
      NSLog(@"Have elements"); 
     } 
+0

不工作,结果始终是“没有任何元素” – mursang

1

尝试像这样使用快速枚举: -

NSArray *separatedText=[NSArray arrayWithObjects:@"one",@"two",@"three",nil]; 
NSArray *separatedLevel=[NSArray arrayWithObjects:@"one",@"two",nil]; 
NSString *str1; 
NSString *str2; 
BOOL isMatch; 

for (str1 in separatedText){ 
    isMatch=NO; 
    for (str2 in separatedLevel){ 
     if (![str1 caseInsensitiveCompare:str2]) 
     { 
      NSLog(@"OK"); 
      isMatch=YES; 
     } 
    } 
    if (isMatch==NO) 
    { 
     NSLog(@"NO"); 
    } 
} 
+0

工作但与我的代码具有相同的结果。查看我的编辑 – mursang

+0

查看代码是否正确,但是它的工作原理是首先遍历循环元素的第一个元素,第一个循环元素的第一个元素就像它正在遍历的那样。所以输出即将在您编辑。 –

+0

我修改了一些代码。请按照 –

相关问题