Ios 检测到“错误”;访问代码“;NSString中会议电话号码的一部分

Ios 检测到“错误”;访问代码“;NSString中会议电话号码的一部分,ios,ios7,nsdatadetector,Ios,Ios7,Nsdatadetector,给定一个表示iOS设备上日历项的事件,我尝试检测的不仅是电话号码,还有相关的会议呼叫接入代码 例如,假设: NSString *text = @"Blah Blah Blah (555) 123-4567 Access Code: 9876# Blah Blah Blah"; 我想实现一个返回此值的方法: @"555-123-4567,,9876#" 日历条目可能会使用除访问代码以外的各种文本:来描述用于访问会议电话的号码。例如,其他字符串可能包括@“密码:,@“虚拟房间号:”等 我知道的功

给定一个表示iOS设备上日历项的事件,我尝试检测的不仅是电话号码,还有相关的会议呼叫接入代码

例如,假设:

NSString *text = @"Blah Blah Blah (555) 123-4567 Access Code: 9876# Blah Blah Blah";
我想实现一个返回此值的方法:

@"555-123-4567,,9876#"
日历条目可能会使用除访问代码以外的各种文本:来描述用于访问会议电话的号码。例如,其他字符串可能包括
@“密码:
@“虚拟房间号:
”等

我知道的功能是检测电话号码,但鉴于上面的示例字符串,它返回
@“555-123-4567访问代码:9876#”
(注意这里的“访问代码”仍然是一个子字符串)

我正在考虑只取从NSDataDetector返回的值,并对
@“Access code:”
和可能出现在日历条目上的类似子字符串进行字符串替换,替换为
@“,”
,但这似乎不够优雅。我更喜欢NSDataDetector为我做这件事

以下是我不太满意的代码:

- (NSArray *) phonesFromText:(NSString *)text {
    NSMutableArray *phones = [NSMutableArray array];

    if (text) {
        NSError *error = nil;
        NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber
                                                                   error:&error];

        [detector enumerateMatchesInString:text
                                   options:kNilOptions
                                     range:NSMakeRange(0, [text length])
                                usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                                    NSString *phoneNumberWithText = result.phoneNumber;

                                    // Replace "access code:" with ",," so that it can be dialed by the built-in phone app
                                    NSString *phoneNumberWithoutText = [phoneNumberWithText stringByReplacingOccurrencesOfString:@"access code:"
                                                                                                                      withString:@",,"
                                                                                                                         options:NSCaseInsensitiveSearch
                                                                                                                           range:NSMakeRange(0, [phoneNumberWithText length])];

                                    // Remove any unwanted characters that may still be lingering after the above string replacement
                                    NSCharacterSet * toRemove = [[NSCharacterSet characterSetWithCharactersInString:@"+,#-0123456789"] invertedSet];
                                    phoneNumberWithoutText = [[phoneNumberWithoutText componentsSeparatedByCharactersInSet:toRemove] componentsJoinedByString:@""];                                        

                                    [phones addObject:phoneNumberWithoutText];
                                }];

    }

    return phones;
}

问题:除了我上面提出的字符串替换解决方案外,还有更好的方法吗?

您有解决方案吗?