Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stringByReplacingOccurencesOfString() for Swift 3.0

Tags:

xcode

swift

I'm having a problem when using stringByReplacingOccurencesOfString() into the latest version of Xcode 8.2 Beta. When using the function, Xcode is showing that the value type of string has no member.

var clockworkSmsUrl = "https://api.clockworksms.com/http/send.aspx?"
                       + "key=123456789abcd"
                       + "to="
                       + usersNumber!
                       + "&content="
                       + usersTextMessage!

clockworkSmsUrl.stringByReplacingOccurrencesOfString(" ", withString: "+")
var clockworkSmsURLconvert = NSURL(string: clockworkSmsUrl)

Any ideas?

like image 930
Kitson88 Avatar asked Jul 11 '16 12:07

Kitson88


1 Answers

You can fix your snippet using ...

clockworkSmsUrl.replacingOccurrences(of: " ", with: "+")

Swift 3 changed the way how objc APIs are imported.

Please note that there are some issues with the code you posted:

  • the result of replacingOccurrences is discarded.
  • there are more characters to be escaped.
  • URLCompontents exposes safer ways to construct URLs.
like image 121
Nikolai Ruhe Avatar answered Oct 17 '22 22:10

Nikolai Ruhe