NSDataDetector检测到“电话号码".文本 [英] NSDataDetector detecting "phone number" text

查看:112
本文介绍了NSDataDetector检测到“电话号码".文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以用代码示例及其输出来解释这个问题的最简单方法,但实际上发生的是NSDataDetector正在检测包含单词"phone number"的字符串中的电话号码.

The easiest way I can explain this problem is with a code sample and its output, but essentially what's happening is NSDataDetector is detecting a phone number within a string that includes the words "phone number".

NSError *error = nil;
NSDataDetector *dataDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber
                                                               error:&error];

NSArray *stringsToTest = @[
                           @"testing phone number 0123 4567891",
                           @"testing some other number 0123 4567892",
                           @"phone number 0123 4567893",
                           @"blah blah 0123 4567894",
                           @"testing telephone number 0123 4567895"
                           ];

for (NSString *string in stringsToTest)
{
    [dataDetector enumerateMatchesInString:string
                                   options:0
                                     range:NSMakeRange(0, string.length)
                                usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                                    NSLog(@"%@", result.phoneNumber);
                                }];
}

输出:

2013-11-24 19:04:26.000 PhoneNumberDetector[21874:70b] phone number 0123 4567891
2013-11-24 19:04:26.000 PhoneNumberDetector[21874:70b] 0123 4567892
2013-11-24 19:04:26.000 PhoneNumberDetector[21874:70b] phone number 0123 4567893
2013-11-24 19:04:26.000 PhoneNumberDetector[21874:70b] 0123 4567894
2013-11-24 19:04:26.001 PhoneNumberDetector[21874:70b] 0123 4567895

我已经阅读了文档并进行了搜索,但是找不到任何指示这是否是预期的行为,如果是的话,为什么的事.

I've read the documentation and searched around SO but can't find anything that indicates if this is the intended behaviour and, if so, why.

这会在7.0模拟器以及运行7.0.4的iPhone 5s上发生.

This happens on the 7.0 simulator as well as my iPhone 5s running 7.0.4.

如果有人能在这个问题上有所作为,我将不胜感激.

If anyone can shed any light on this issue I'd greatly appreciate it.

更新: 为了澄清,访问NSTextCheckingResult phoneNumber属性时,我不希望文本电话号码"成为结果的一部分.

Update: To clarify, I don't want the text "phone number" to be part of the result when accessing the NSTextCheckingResult phoneNumber property.

推荐答案

虽然这似乎是Apple代码中的错误,但是如果您想通过消除非字符串来仅从字符串中提取数字,则有一个简单的解决方法-块中的数字字符:

While it does seem that this is a bug in Apple's code, there is a simple workaround if you want to extract just the number from the string by eliminating the non-numeric characters within a block:

NSError *error = nil;
    NSDataDetector *dataDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber
                                                                   error:&error];

    NSArray *stringsToTest = @[
                                @"testing phone number 0123 4567891",
                                @"testing some other number 0123 4567892",
                                @"phone number 0123 4567893",
                                @"blah blah 0123 4567894",
                                @"testing telephone number 0123 4567895",
                                @"phone",
                                @"number",
                                @"my phone number 1234587985",
                                @"this string does not contain a phone number"
                               ];

    for (NSString *string in stringsToTest)
    {
        [dataDetector enumerateMatchesInString:string
                                       options:0
                                         range:NSMakeRange(0, string.length)
                                    usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) 
                                    {
                                        NSString *numberWithExtra = result.phoneNumber;
                                        NSCharacterSet *toRemove = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
                                        NSString *trimmed = [[numberWithExtra componentsSeparatedByCharactersInSet:toRemove] componentsJoinedByString:@""];
                                        if(trimmed && trimmed.length)
                                        {
                                            NSLog(@"%@", trimmed);
                                        }
                                        else
                                        {
                                            NSLog(@"No phone number");
                                        }
                                    }];
    }

这篇关于NSDataDetector检测到“电话号码".文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆