RegexKitLite - iOS设备:无法识别的选择
问题描述:
我有必须使用正则表达式的一个项目,所以我决定用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'
我错过了什么?请帮助我..
答
经过一番挖掘之后,实际上并没有任何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);
}
没有ü声明你头文件:在您的实现文件#进口“RegexKitLite.h”? – tiguero 2013-02-28 17:58:27
我已经将“RegexKitLite.h”导入到我的标题类 – LightNight 2013-02-28 18:00:42
它适用于Mac OS还是iOS,因为这个lib似乎只适用于MacOS。您还试图导入iOS上不允许的动态库。 – tiguero 2013-02-28 18:08:29