Strange Invisible Character in String causing NSURL to fail

旧街凉风 提交于 2020-01-17 07:12:21

问题


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?


回答1:


You have to use .stringByAddingPercentEscapesUsingEncoding to convert special characters.

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

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


来源:https://stackoverflow.com/questions/28734589/strange-invisible-character-in-string-causing-nsurl-to-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!