我正在使用 anNSDataDetector
从字符串中获取地址。我正在调用该方法大约 500 次,它会产生明显的延迟(在 iPhone 4 上大约 3 秒)。有什么办法可以加快速度吗?我尝试将数据检测器移动到静态变量,认为设置需要时间,但没有任何区别。这是代码:
- (NSDictionary *)addressDetectorForString:(NSString *)address
{
if (!address) return nil;
static NSDataDetector *addressDetector = nil;
if (!addressDetector) {
addressDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeAddress error:nil];
}
NSDictionary *dictionary = nil;
NSArray* matches = [addressDetector matchesInString:address options:0 range:NSMakeRange(0, [address length])];
if ([matches count] > 0) {
for (NSTextCheckingResult *match in matches) { // Only ever one result.
if ([match resultType] == NSTextCheckingTypeAddress) {
dictionary = [match addressComponents];
}
}
}
return dictionary;
}