ios - Array empty before finishing the method it -
i have made mistake can't find. array (class level) gets empty before returning it.
private func loaduserdata() { // 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) print(self.userdata) } } } print(userdata) }
i'm trying access values other part of code , can't gets empty. can see array data first time print it, not in second one. thank much!
explanation of op's question extending @the4kman answer.
your code in dispatch/block. dispatch/block called after time.
so first print called , @ time userdata
empty. in second case print called in block , assigns data userdata
. when second print called userdata
have values.
please check below code mentioned first , second print called accordingly.
for practical , put break points on both print line , check line called when.
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) print(self.userdata) // 2. print second } } } print(userdata) // 1. print first }
explanation of @the4kman answer
he used callback i.e. callback used data in calling function when received data in called function. here in answer not use param, can send array.
note in called func getting data in block. data within block situation have use completionhandler.
please take refer:
how completion handler work on ios?
edit
in func, make changes this.
func loaduserdata( completion: (myarray: [string]) -> void) { // array array of string. assuming you'r passing array of string closure. // write below line wanted call callback or return through closure. // recommandated use below line in second print. completion(userdata) // 'userdata' array wanted pass closure. }
how call closure.
loaduserdata { (result: [string]) in print("got back: \(result)") }
note : may need change closure @escaping
, x-code gives suggestion. first try edit answer.
Comments
Post a Comment