[英]NSDataDetector to detect a name in a string
我一直在努力理解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
}
}
您的代码使用一个寻找地址的数据检测器。 您的字符串中没有地址。 因此,数据检测器找不到任何东西。
没有用于查找名称的数据检测器。
如果您知道字符串的结构,请使用它。 例如,如果您可以像示例中那样保证名称前始终有“ space-slash-space”,那么请搜索该名称,然后将其作为名称。
如果您不知道字符串的结构,则该问题几乎无法解决。
NSDataDetector不支持名称检测。 为此 ,请使用NSLinguisticTagger 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.