ios - Swift 3 and Alamofire - Image and data from JSON -
i have project trying bring in data web url using alamofire. trying bring in image , text keep getting build failed. trying add data (image , text) tags = 1 , 2. code below appreciated. new swift.
swift
import uikit import alamofire struct postinput { let mainimage : uiimage! let name : string! } class tableviewcontroller: uitableviewcontroller { var postsinput = [postinput]() var mainurl = "https://www.testjson.com" typealias jsonstandard = [string : anyobject] override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. callalamo(url: mainurl) } func callalamo(url : string){ alamofire.request(url).responsejson(completionhandler: { response in self.parsedata(jsondata: response.data!) }) } func parsedata(jsondata : data) { { var readablejson = try jsonserialization.jsonobject(with: jsondata, options: .mutablecontainers) as! jsonstandard if let posts = readablejson["posts"] as? [jsonstandard] { post in posts { let title = post["title"] as! string if let images = post["image"] as? jsonstandard { let mainimageurl = url(string: imagedata["url"] as! string) let mainimagedata = nsdata(contentsof: mainimageurl!) let mainimage = uiimage(data: mainimagedata as! data) } postsinput.append(postinput.init(mainimage: mainimage, name: name)) } self.tableview.reloaddata() } } catch { print(error) } } override func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int { return postsinput.count } override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecell(withidentifier: "cell") // cell?.textlabel?.text = titles[indexpath.row] let mainimageview = cell?.viewwithtag(2) as! uiimageview mainimageview.image = postsinput[indexpath.row].mainimage let mainlabel = cell?.viewwithtag(1) as! uilabel mainlabel.text = postsinput[indexpath.row].name return cell! } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }
json
{ posts: [ { id: “000000”, url: "/content/interview2”, date: "2016-11-03 09:01:41", modified: "2016-11-03 09:03:47", title: "an interview", image: "https://www.example.com/sites/default/files/oregood.jpeg", summary: { value: "<p>latin text here</p> ", format: "filtered_html" } ]}
the following reasons build error following:
you added line
postsinput.append(postinput.init(mainimage: mainimage, name: name))
outside contextmainimage
obtained. based on json, believe supposed rewrite this.if let imageurl = post["image"] as? jsonstandard { let mainimageurl = url(string: imageurl) let mainimagedata = nsdata(contentsof: mainimageurl!) let mainimage = uiimage(data: mainimagedata as! data) postsinput.append(postinput.init(mainimage: mainimage, name: title)) }
even if that, variables
imagedata
,name
undefined. should that.just suggestion, not force unwrap optionals. use optional binding wherever possible.
Comments
Post a Comment