我正在使用NSDataDetector
从字符串中获取地址。我打算用这种方法做大约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;
}