简体   繁体   中英

Strange Invisible Character in String causing NSURL to fail

I am having a String to Number problem that is taking me days to resolve. So it all started out in my app that involves taking a phone number from the addressbook and making a call. Everything was working fine until I noticed some phone numbers were causing my program to crash.

After some time, I finally figured that they were crashing for the phone numbers that had a space in it, which finally made sense because even though it is a phone number I am using NSURL to call "tel://(555) 555 5555" and having a space between it will make the URL invalid which was causing my program to crash.

Now I was happy I figured out the second problem and decided to do a simple character replacement to replace all whitespace with "-". This worked for the test string I tested it on and everything looked good. I decided to hook this back up to my code and then my program is still crashing.

I then did some debugging and noticed even though my test case worked, the phone numbers I am pulling from the iOS address book were not having the whitespace replaced. It seems that whatever character is being used to space out the phone digits is not a whitespace or at least it is not a whitespace that I can simply type like this -> " "

Any idea how to resolve this? Either (1) to know what exactly this invisible character is and replace it or (2) is there a way to just extract a phone number as all numbers instead of the extra chars "+(.)- " or any option here?

You have to use .stringByAddingPercentEscapesUsingEncoding to convert special characters.

let myLink = "http://google.com"

let myUrl = NSURL(string: myLink.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM