json - How to unwrap optional in swift3 -


this question has answer here:

var people = [mydata]()  override func prepare(for segue: uistoryboardsegue, sender: any?) {       if segue.identifier == "theresegue" ,         let destinationnavigationcontroller = segue.destination as? uinavigationcontroller ,      let nextscene = destinationnavigationcontroller.topviewcontroller as? commentview ,          let indexpath = self.mytable.indexpathforselectedrow {         let selectedvehicle = people[indexpath.row]          nextscene.postid = (selectedvehicle.value(forkey: "id") as? string)!         print(nextscene.postid)                  nextscene.posttype = (selectedvehicle.value(forkey: "ptype") as? string)!         print(nextscene.posttype)     } } 

when trying do

let jsonurl = "https://www.zdoof.com/api/ztimeline/add_post_comment?" let jsonurlwithparameters = jsonurl + "comment=\(string(describing: self.commenttext.text!))" + "&post_id=\(postid)" + "&post_type=\(posttype)" + "&created_by=4490" + "&created_on=\(joinstring)" print(jsonurlwithparameters) 

it printing

optional(8167)

optional(1)

https://www.zdoof.com/api/ztimeline/add_post_comment?comment=hzbxhjxjx&post_id=optional(8167)&post_type=optional(1)&created_by=4490&created_on=18%2faug%2f2017+17%3a42%3a43

so how delete "optional"

simply put ! after optional value want unwrap.

ex:

var integer: int? = 5  print(integer) // print out optional(5)  print(integer!) // print out 5 

you should careful though; rather use

if let unwrappedvalue = integer {   print(unwrappedvalue) } 

that prints out value if it's not optional.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -