码迷,mamicode.com
首页 > 其他好文 > 详细

NSDataDetector和NSTextCheckingResult的使用

时间:2014-12-15 12:16:29      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   使用   sp   for   on   数据   

NSDataDetector是NSRegularExpression的子类,主要用于检测半结构化的数据:日期,地址,电话号码,正则表达式等等。

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
};
使用方法

    NSError *error = nil;
    //根据检测的类型初始化NSDataDetector
    NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber | NSTextCheckingTypeLink
                                                               error:&error];
    //需要检测的字符串
    NSString *testStr = @"有一个网址:wwww.JohnnyLiu.com有一个电话:15310547654 还有一个地址:大屯路";
    //可以有多种方法检测匹配的数据
    //1.检测然后对每个检测到的数据进行操作
    //- (void)enumerateMatchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range usingBlock:(void (^)(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop))block;
    //2.检测获得检测得到的数组
    //- (NSArray *)matchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range;
    //3.获得检测得到的总数
    //- (NSUInteger)numberOfMatchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range;
    //4.第一个检测到的数据
    //- (NSTextCheckingResult *)firstMatchInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range;
    //5.第一检测到的数据的Range
    //- (NSRange)rangeOfFirstMatchInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range;
    
    [detector enumerateMatchesInString:testStr
                               options:kNilOptions
                                 range:NSMakeRange(0, testStr.length)
                            usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                                //NSTextCheckingResult有很多属性,和检测的类型相匹配,如URL,phoneNumber,date,addressComponents等等
                                //NSTextCheckingResult还有Range 和 NSTextCheckingType两个属性,方便进行操作
                                NSLog(@"result.range = %@",NSStringFromRange(result.range));
                                if (result.URL) {
                                    NSLog(@"url = %@",result.URL);
                                }
                                if (result.phoneNumber) {
                                    NSLog(@"phone = %@",result.phoneNumber);
                                }
    }];
    //结果-------------------------------------------
    //result.range = {6, 18}
    //url = http://wwww.JohnnyLiu.com
    //result.range = {27, 14}
    //phone = 电话:15310547654

NSDataDetector和NSTextCheckingResult的使用

标签:style   http   io   ar   使用   sp   for   on   数据   

原文地址:http://blog.csdn.net/lcl130/article/details/41940861

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!