2

I have following code in func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool

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
}

Phone number has only 10 digit. But when I check the count it shows 12 Digits

enter image description here

When I print the string

enter image description here

I have tried

phoneNumber?.trimmingCharacters(in: .whitespacesAndNewlines).count phoneNumber?.trimmingCharacters(in: .symbols).count

but it still returns 12

Please help

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98

2 Answers2

1

Thank you every one for your help. You are so kind

I am able to find the invalid character using

phoneNumber?.map{$0.unicodeScalars.allSatisfy{$0.isASCII}}

and it returns

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

I am able to fix this issue using one line

phoneNumber?.filter{$0.unicodeScalars.allSatisfy{$0.isASCII}}

and Count is 10

using

phoneNumber?.filter{$0.unicodeScalars.allSatisfy{$0.isASCII}}.count

Hope it is helpful to others :)

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
0
extension String {
    var specialCharRemove: String {
       let characters = Set("1234567890+-=().!_")
       return self.filter {characters.contains($0)}
    }
}

Use:

let str = "12345.!". specialCharRemove

Result:

12345

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174