0

What I am doing is shipping a JSON structured string on NSULRRequest, getting rid of the urlscheme prefix, serialize it to JSON and then whatever I can do with it.

urlscheme://{"Type":"photo","Name":"Photo13"}

So I changed NSURLRequest to string by doing

    let url = request.URL
    let string = "\(url)"
    print(string)

but the string appears like

urlscheme://%7B%22Type%22:%22photo%22,%22Name%22:%22Photo13%22%7D

So I have to change it by invoking stringByReplacingOccurrencesOfString over and over on all possible changes.

replacedString = replacedString.stringByReplacingOccurrencesOfString("%7B",
        withString: "{",
        options: NSStringCompareOptions.LiteralSearch,
        range: nil)
.
..
...

Is there a better way to do this in Swift?

Kyle KIM
  • 1,384
  • 2
  • 15
  • 31

1 Answers1

1

What you've got there is a percent encode string. This is done because certain characters have special meanings within a URL. The NSString method stringByRemovingPercentEncoding will convert a percent encode string back to a normal string for you.

See this answer

Hugo Tunius
  • 2,869
  • 24
  • 32
  • Thank you. Xcode7 just let me know that method is now deprecated and use stringByRemovingPercentEncoding instead. – Kyle KIM Sep 27 '15 at 22:55