NSTextCheckingResult用于电话号码

编程入门 行业动态 更新时间:2024-10-26 15:29:35
本文介绍了NSTextCheckingResult用于电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有人可以告诉我为什么每次评估为真??

Can someone tell me why this evaluates every time to true?!

输入为: jkhkjhkj 。我在电话字段中键入的内容无关紧要。每次都是真的......

The input is: jkhkjhkj. It doesn't matter what I type into the phone field. It's every time true...

NSRange range = NSMakeRange (0, [phone length]); NSTextCheckingResult *match = [NSTextCheckingResult phoneNumberCheckingResultWithRange:range phoneNumber:phone]; if ([match resultType] == NSTextCheckingTypePhoneNumber) { return YES; } else { return NO; }

这是匹配:

(NSTextCheckingResult *) $4 = 0x0ab3ba30 <NSPhoneNumberCheckingResult: 0xab3ba30>{0, 8}{jkhkjhkj}

我使用的是RegEx和 NSPredicate 但是我我读过,因为iOS4建议使用 NSTextCheckingResult ,但我找不到任何好的教程或示例。

I was using RegEx and NSPredicate but I've read that since iOS4 it's recommended to use NSTextCheckingResult but I can't find any good tutorials or examples on this.

提前致谢!

推荐答案

您正在错误地使用该课程。 NSTextCheckingResult 是文本检查的结果,由 NSDataDetector 或 NSRegularExpression 。改为使用 NSDataDetector :

You are using the class incorrectly. NSTextCheckingResult is the result of a text checking that is done by NSDataDetector or NSRegularExpression. Use NSDataDetector instead:

NSError *error = NULL; NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:&error]; NSRange inputRange = NSMakeRange(0, [phone length]); NSArray *matches = [detector matchesInString:phone options:0 range:inputRange]; // no match at all if ([matches count] == 0) { return NO; } // found match but we need to check if it matched the whole string NSTextCheckingResult *result = (NSTextCheckingResult *)[matches objectAtIndex:0]; if ([result resultType] == NSTextCheckingTypePhoneNumber && result.range.location == inputRange.location && result.range.length == inputRange.length) { // it matched the whole string return YES; } else { // it only matched partial string return NO; }

更多推荐

NSTextCheckingResult用于电话号码

本文发布于:2023-10-27 22:37:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534756.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:电话号码   NSTextCheckingResult

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!