ios - Unexpectedly found nil while unwrapping an Optional value - NSMutableURLRequest -
when try run below code snippet works !
let urlwithparams = "http://192.168.0.4:3003/manager/all" let request = nsmutableurlrequest(url: nsurl(string: urlwithparams)!)
but when string settings.bundle textfield below code doesn't work :
let webserver:string = nsuserdefaults().stringforkey("priceweb")! serverresponse.appendcontentsof(webserver) serverresponse.appendcontentsof("/manager/all") let request = nsmutableurlrequest(url: nsurl(string: serverresponse)!)
when execute
print(webserver);
the output http://192.168.0.4:3003
, when execute
print(serverresponse);
the output http://192.168.0.4:3003/manager/all
but still error appears in following line:
let request = nsmutableurlrequest(url: nsurl(string: serverresponse)!)
fatal error: unexpectedly found nil while unwrapping optional value
note : please provide answers in swift
you must encode url contains special characters.
try this
let urlwithparams = "http://192.168.0.4:3003/manager/all" let urlstr = urlwithparams.stringbyaddingpercentescapesusingencoding(nsutf8stringencoding)! // or use let urlstr = urlwithparams.stringbyaddingpercentencodingwithallowedcharacters(.urlqueryallowedcharacterset()) let request = nsmutableurlrequest(url: nsurl(string: urlstr)!)
for more reference see apple documents
Comments
Post a Comment