Replacing character in string with Swift

It may look like a little thing that may not even worth to write on my blog but this little stuff just saved me tons of time and a huge panic attack while i was working on coding some http requests for a iPhone app. So here it is:

var newsParam = "Aston Martin"
var convertedNewsParam = newsParam.stringByReplacingOccurrencesOfString(" ", withString: "%20")
var webservicePath = "http://xx.xx.xx.xx:yy/myWebService?search_text=\(convertedNewsParam)"

Some may say, “Why don’t you request by using id?”. That’s what i said my colleagues but their code was working this way and they didn’t want to change it since it’s working. So, when i just copy-pasted url to Postman with request parameter as “Aston Martin” it was working fine but it was giving me this error message: fatal error: unexpectedly found nil while unwrapping an Optional value

But when i changed space charater to it’s html equivalent (which is “%20” as you see from the code) and change my request parameter to Aston%20Martin it sets us free from that fatal error.

Yorum bırakın

Filed under Swift

Bir Cevap Yazın

Aşağıya bilgilerinizi girin veya oturum açmak için bir simgeye tıklayın:

WordPress.com Logosu

WordPress.com hesabınızı kullanarak yorum yapıyorsunuz. Log Out / Değiştir )

Twitter resmi

Twitter hesabınızı kullanarak yorum yapıyorsunuz. Log Out / Değiştir )

Facebook fotoğrafı

Facebook hesabınızı kullanarak yorum yapıyorsunuz. Log Out / Değiştir )

Google+ fotoğrafı

Google+ hesabınızı kullanarak yorum yapıyorsunuz. Log Out / Değiştir )

Connecting to %s