ios - Cannot asign value type '()' to type String? -
i trying information request using completionhandler. thing need array information other part of code , seems can't access it. here access data:
private func loaduserdata(completionhandler: @escaping ([string]) -> ()) { // we're getting user info data json response let session = twitter.sharedinstance().sessionstore.session() let client = twtrapiclient.withcurrentuser() let userinfourl = "https://api.twitter.com/1.1/users/show.json" let params = ["user_id": session?.userid] var clienterror : nserror? let request = client.urlrequest(withmethod: "get", url: userinfourl, parameters: params, error: &clienterror) client.sendtwitterrequest(request) { (response, data, connectionerror) -> void in if connectionerror != nil { print("error: \(connectionerror)") } { let json = json(data: data!) if let username = json["name"].string, let description = json["description"].string, let followerscount = json["followers_count"].int, let favouritescount = json["favourites_count"].int, let followingcount = json["friends_count"].int, let lang = json["lang"].string, let nickname = json["screen_name"].string { self.userdata.append(username) self.userdata.append(description) self.userdata.append(string(followerscount)) self.userdata.append(string(favouritescount)) self.userdata.append(string(followingcount)) self.userdata.append(lang) self.userdata.append(nickname) completionhandler(self.userdata) } } } } func manageuserdata(index: int) { loaduserdata() {_ in return self.userdata[index] } }
and here need data show it:
override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let rownumber = indexpath.row let cellidentifier = "tableviewcell" guard let cell = tableview.dequeuereusablecell(withidentifier: cellidentifier, for: indexpath) as? tableviewcellcontroller else { fatalerror("the dequeued cell not instance of tableviewcellcontroller.") } switch rownumber { case 0: cell.titleplaceholder.text = "name:" cell.valueplaceholder.text = manageuserdata(index: 1)
in last line it's error occurs: "cannot asign value of type '()' type string?. not sure if using completionhandler in order retrieve data , ensure can access different parts of code.
any idea ? thank much!
update not using completionhandler properly. changed :
private func loaduserdata(completion: @escaping (_ myarray: [string]) -> void)
so manageuserdata function looks this:
func manageuserdata() { loaduserdata { (result: [string]) in self.seconduserdata = result } }
hope helps!
since async network call, should start call in vc's viewwillappear
, viewdidappear
, or viewdidload
-- depending on if need data reload.
override func viewwillappear(_ animate: bool) { super.viewwillappear(animate) sessionmanager.loaduserdata { (strings) in } }
then inside of loaduserdata
closure ran whenever call complete, load data array used tableview's datasource , call tableview.reloaddata()
sessionmanager.loaduserdata { (strings) in self.datasource = strings self.tableview.reloaddata() }
then cellforrow
need:
override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let rownumber = indexpath.row let cellidentifier = "tableviewcell" guard let cell = tableview.dequeuereusablecell(withidentifier: cellidentifier, for: indexpath) as? tableviewcellcontroller else { fatalerror("the dequeued cell not instance of tableviewcellcontroller.") } switch rownumber { case 0: cell.titleplaceholder.text = "name:" cell.valueplaceholder.text = datasource[0] } }
make sure numberofrows
returning datasource.count
Comments
Post a Comment