到目前为止,我一直在努力理解NSDataDetector类。我已经阅读了文档,但就是不能掌握它。我所做的每一件事Xcode都告诉我有一个错误。
我觉得我这次尝试可能走在正确的道路上。如何编写一个简单的基础程序来查找字符串中的名称?
NSError *error = nil;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeAddress error:&error];
NSString *string = @"(555) 555-5555 / Nick";
NSArray *matches = [detector matchesInString:string
options:0
range:NSMakeRange(0, [string length])];
for (NSTextCheckingResult *match in matches) {
if([match addressComponents] // contains NSTextCheckingNameKey){
// do this
}
}发布于 2014-01-27 11:45:12
您的代码使用查找地址的数据检测器。您的字符串中没有地址。因此,数据检测器找不到任何东西。
没有用于查找名称的数据检测器。
如果你知道字符串的结构,那么就使用它。例如,如果您可以保证名称前总是有"space-slash-space“,则搜索该名称,并将后面的内容作为名称。
如果你不知道字符串的结构,那么这个问题几乎是无法解决的。
发布于 2014-03-19 18:48:54
NSDataDetector不支持名称检测。为此,请使用NSLinguisticTagger。
https://stackoverflow.com/questions/21372292
复制相似问题