swift - Can I fix this with the using of API Spotify in iOS app? -


i have error:

unexpectedly found nil while unwrapping optional value"

at line: vc.mainpreviewurl = posts[indexpath!].previewurl

how can fix please? code,thanks in advance ;)

class tableviewcontroller: uitableviewcontroller {      var posts = [post]()     var names = [string]()     var searchurl = "https://api.spotify.com/v1/search?q=shawn+mendes&type=track"     var oauthtoken = "bqcvqhznohyhgutkvw43pdxv4yzs9jhvdipsn3xbxne5jbg0zwnrpfwh81vmeuk5lqerel0djajt1iyla1t9yzqmdypc5lkmd5z_ndzeawrcevh4fmc_nn50x2r_i8a38amrjfms8qpnhgyohjae8sfvjbswqoererr2rrebmxc8jmgq7-aq-ttalp87ducrvy8mt8wvt8muenihus8hxrctt071x7he2j_eghjswp7woa5foyk9xhzkxu_p_3hkab6x6rbycm4sfx9wldtb5h_jikfeht-15mjol_pmnryo9wpnaclkts3aobldlnk"      typealias jsonstandard = [string: anyobject]      override func viewdidload() {         super.viewdidload()         callalamo(url: searchurl,token: oauthtoken)     }      func callalamo(url : string,token: string){          alamofire.request(searchurl, method: .get, parameters: ["q":"shawn mendes", "type":"track"], encoding: urlencoding.default, headers: ["authorization": "bearer "+oauthtoken]).responsejson { response in             self.parsedata(jsondata: response.data!)             print(response)         }     }       func parsedata(jsondata:data){         {             var readablejson = try jsonserialization.jsonobject(with: jsondata, options: .mutablecontainers) as! jsonstandard             print(readablejson)             if let tracks = readablejson["tracks"] as? jsonstandard{                 if let items = tracks["items"] as? [jsonstandard] {                     in 0..<items.count{                         let item = items[i]                         print(item)                         let name = item["name"] as! string                         let previewurl = item["preview_url"] as? string                         if let album = item["album"] as? jsonstandard {                             if let images = album["images"] as? [jsonstandard]{                                 let imagedata = images [0]                                 let mainimageurl = url(string: imagedata["url"] as! string)                                 let mainimagedata = nsdata(contentsof: mainimageurl!)                                 let mainimage = uiimage(data: mainimagedata as! data)                                 posts.append(post.init(mainimage: mainimage! ,name: name,previewurl: previewurl))                                 self.tableview.reloaddata()                             }                         }                     }                 }             }         }         catch {             print(error)         }     }     override func prepare(for segue: uistoryboardsegue,sender: any?){         let indexpath = self.tableview.indexpathforselectedrow?.row         let vc = segue.destination as! audiovc         vc.image = posts[indexpath!].mainimage         vc.mainsongtitle = posts[indexpath!].name         vc.mainpreviewurl = posts[indexpath!].previewurl     } } 

swift 3x change line ...

override func prepare(for segue: uistoryboardsegue,sender: any?){ if let indexpath = self.tableview.indexpathforselectedrow{     let vc = segue.destination as! audiovc     vc.image = posts[indexpath.row].mainimage     vc.mainsongtitle = posts[indexpath.row].name     vc.mainpreviewurl = posts[indexpath.row].previewurl  } } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -