我在type PriceType = 'wholesale' | 'retail';
type AlwaysProps = $ReadOnly<{|
label: string,
|}>;
type Props = $ReadOnly<{|
...AlwaysProps,
hasPrice: true,
priceType: PriceType,
|}> | $ReadOnly<{|
...AlwaysProps,
hasPrice: false,
|}>;
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool
电话号码只有10位数字。但是当我检查计数时,它显示12位数字
当我打印字符串
我尝试过
var phoneNumber: String?
if let callIntent = interaction.intent as? INStartVideoCallIntent {
phoneNumber = callIntent.contacts?.first?.personHandle?.value
} else if let callIntent = interaction.intent as? INStartAudioCallIntent {
phoneNumber = callIntent.contacts?.first?.personHandle?.value
}
phoneNumber?.trimmingCharacters(in: .whitespacesAndNewlines).count
但它仍然返回12
请帮助
答案 0 :(得分:1)
谢谢大家的帮助。你真好
我可以使用
查找无效字符 phoneNumber?.map{$0.unicodeScalars.allSatisfy{$0.isASCII}}
它返回
Optional<Array<Bool>>
▿ some : 12 elements
- 0 : false
- 1 : true
- 2 : true
- 3 : true
- 4 : true
- 5 : true
- 6 : true
- 7 : true
- 8 : true
- 9 : true
- 10 : false
- 11 : true
我可以使用一行来解决此问题
phoneNumber?.filter{$0.unicodeScalars.allSatisfy{$0.isASCII}}
计数为10
使用
phoneNumber?.filter{$0.unicodeScalars.allSatisfy{$0.isASCII}}.count
希望对其他人有帮助:)
答案 1 :(得分:0)
extension String {
var specialCharRemove: String {
let characters = Set("1234567890+-=().!_")
return self.filter {characters.contains($0)}
}
}
使用:
let str = "12345.!". specialCharRemove
结果:
12345