Eir*_*eia 7 string xcode ios swift
嘿,我想检查是否UITextView
(可能这可能是相关的)包含文本中的电话号码.我正在使用Swift 2.3
,但如果你得到它,Swift 3
我会尝试翻译它.
应该为这些输入工作例如:
最后一种情况,如果其他工作很容易,只需将""出现更换为""并检查上一个函数containsPhone.
我尝试过这个解决方案,但不适用于第一种情况:( 查找字符串中的电话号码 - 常规)
谢谢.
vad*_*ian 12
NSDataDetector
提供了一种方便的方法:
let string = "Good morning, 627137152 \n Good morning, +34627217154 \n Good morning, 627 11 71 54"
do {
let detector = try NSDataDetector(types: NSTextCheckingResult.CheckingType.phoneNumber.rawValue)
let numberOfMatches = detector.numberOfMatches(in: string, range: NSRange(string.startIndex..., in: string))
print(numberOfMatches) // 3
} catch {
print(error)
}
Run Code Online (Sandbox Code Playgroud)
或者如果你想提取数字
let matches = detector.matches(in: string, range: NSRange(string.startIndex..., in: string))
for match in matches{
if match.resultType == .phoneNumber, let number = match.phoneNumber {
print(number)
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1811 次 |
最近记录: |