![](/style/images/good.png)
![](/style/images/bad.png)
初识 NSDataDetector
source link: https://www.isaced.com/post-214.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
初识 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
- duration
- timeZone
NSTextCheckingTypeAddress
- addressComponents
- NSTextCheckingNameKey
- NSTextCheckingJobTitleKey
- NSTextCheckingOrganizationKey
- NSTextCheckingStreetKey
- NSTextCheckingCityKey
- NSTextCheckingStateKey
- NSTextCheckingZIPKey
- NSTextCheckingCountryKey
- NSTextCheckingPhoneKey
- NSTextCheckingTypeLink
NSTextCheckingTypePhoneNumber
- phoneNumber
- components*
- NSTextCheckingTypeTransitInformation
- NSTextCheckingAirlineKey
- NSTextCheckingFlightKey
如果你想在UILabel中简单地使用NSDataDetector,可以看看这个:TTTAttributedLabel 。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK