初识 NSDataDetector

机器说二进制语言,而人类说谜语,半真半假,和疏忽。

在 Cocoa 开发中,有一个简单的对于寻找数据的解决方案:NSDataDetector

NSDataDetector 是继承于 NSRegularExpression(Cocoa 中的正则表达式)的一个子类,你可以把它看作一个正则表达式匹配器和令人难以置信的复杂的表达式,可以从自然语言(虽然可能更复杂)中提取你想要的信息。

看看下面一段例子:

NSError *error = nil;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink | NSTextCheckingTypePhoneNumber error:&error];

NSString *string = @"123 Main www.isaced.com St. / (023) 52261439";
[detector enumerateMatchesInString:string
                           options:kNilOptions
                             range:NSMakeRange(0, [string length])
                        usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                                NSLog(@"Match: %@", result);
        }];

输出结果为:

Match: <NSLinkCheckingResult: 0x7510b00>{9, 14}{http://www.isaced.com}
Match: <NSPhoneNumberCheckingResult: 0x8517140>{30, 14}{(023) 52261439}

可以看到在 Block 中的 NSTextCheckingResult 作为结果输出,

注意:当初始化 NSDataDetector 的时候,只指定自己需要的类型(Type)就可以了,因为多增加一项就会多一些内存的开销。

看了下 NSTextCheckingResult.h 文件,里面可以找到一些系统为你设定好的匹配类型:

typedef NS_OPTIONS(uint64_t, NSTextCheckingType) {    // a single type
    NSTextCheckingTypeOrthography           = 1ULL << 0,            // language identification
    NSTextCheckingTypeSpelling              = 1ULL << 1,            // spell checking
    NSTextCheckingTypeGrammar               = 1ULL << 2,            // grammar checking
    NSTextCheckingTypeDate                  = 1ULL << 3,            // date/time detection
    NSTextCheckingTypeAddress               = 1ULL << 4,            // address detection
    NSTextCheckingTypeLink                  = 1ULL << 5,            // link detection
    NSTextCheckingTypeQuote                 = 1ULL << 6,            // smart quotes
    NSTextCheckingTypeDash                  = 1ULL << 7,            // smart dashes
    NSTextCheckingTypeReplacement           = 1ULL << 8,            // fixed replacements, such as copyright symbol for (c)
    NSTextCheckingTypeCorrection            = 1ULL << 9,            // autocorrection
    NSTextCheckingTypeRegularExpression NS_ENUM_AVAILABLE(10_7, 4_0)  = 1ULL << 10,           // regular expression matches
    NSTextCheckingTypePhoneNumber NS_ENUM_AVAILABLE(10_7, 4_0)        = 1ULL << 11,           // phone number detection
    NSTextCheckingTypeTransitInformation NS_ENUM_AVAILABLE(10_7, 4_0) = 1ULL << 12            // transit (e.g. flight) info detection
};

当然这里只是截取了一部分,具体可以点 dataDetectorWithTypes 方法进入到 NSTextCheckingResult.h 文件中查看。

  • NSTextCheckingTypeDate
    • date
    • duration
    • timeZone
  • NSTextCheckingTypeAddress
    • addressComponents
    • NSTextCheckingNameKey
    • NSTextCheckingJobTitleKey
    • NSTextCheckingOrganizationKey
    • NSTextCheckingStreetKey
    • NSTextCheckingCityKey
    • NSTextCheckingStateKey
    • NSTextCheckingZIPKey
    • NSTextCheckingCountryKey
    • NSTextCheckingPhoneKey
  • NSTextCheckingTypeLink
    • url
  • NSTextCheckingTypePhoneNumber
    • phoneNumber
    • components*
  • NSTextCheckingTypeTransitInformation
    • NSTextCheckingAirlineKey
    • NSTextCheckingFlightKey

如果你想在 UILabel 中简单地使用 NSDataDetector,可以看看这个:TTTAttributedLabel

参考自:http://nshipster.com/nsdatadetector/

comments powered by Disqus