2013-02-28 27 views
-1

我有必須使用正則表達式的一個項目,所以我決定用RegexKitLite,我下載了它,增加了RegexKitLite.m成「編譯來源」,並RegexKitLite.h將「」複製到「複製文件」部分。項目庫增加了libicucore.A.dylib。進口RegexKitLite.h到我的課,寫(只是爲了測試)代碼:RegexKitLite - iOS設備:無法識別的選擇

 NSString *str = @"testing string"; 
     if ([str isMatchedByRegex:@"[^a-zA-Z0-9]"]) 
     { 
      NSLog(@"Some message here"); 
     } 

之後,我有錯誤消息:

-[__NSCFString isMatchedByRegex:]: unrecognized selector sent to instance 0x1ed45ac0 
2013-02-28 19:46:20.732 TextProject[8467:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString isMatchedByRegex:]: unrecognized selector sent to instance 0x1ed45ac0' 

我錯過了什麼?請幫助我..

+0

沒有ü聲明你頭文件:在您的實現文件#進口「RegexKitLite.h」? – tiguero 2013-02-28 17:58:27

+0

我已經將「RegexKitLite.h」導入到我的標題類 – LightNight 2013-02-28 18:00:42

+0

它適用於Mac OS還是iOS,因爲這個lib似乎只適用於MacOS。您還試圖導入iOS上不允許的動態庫。 – tiguero 2013-02-28 18:08:29

回答

1

經過一番挖掘之後,實際上並沒有任何Cocoa API在iOS4之前使用regex,因此程序員正在使用像RegexKitLite這樣的外部庫,它們確實可以用於iOS。

如果你在iOS4或更高版本中,應該沒有任何理由不使用NSRegularExpression。類參考描述可以在here找到。

例如與NSRegularExpression您的代碼段看起來像:

NSString *str = @"testing string"; 
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^a-zA-Z0-9]" options:NSRegularExpressionCaseInsensitive error:nil]; 
NSTextCheckingResult *match = [regex firstMatchInString:str options:0 range:NSMakeRange(0, [str length])]; 
NSRange range = [match rangeAtIndex:0]; 
if (range.location != NSNotFound) 
{ 
    NSString* matchingString = [str substringWithRange:range]; 
    NSLog(@"%@", matchingString); 
}