ios - Swift Converting PFQuery to String Array for TableView -
i trying query of parse users in database , display each individual user in own cell in tableview. have set tableview, i'm stuck on saving user query string array can used within tableview. have created loadparsedata
function finds objects in background , appends objects queried string array. unfortunately given error message on line append data.
implicit user of 'self' in closure; use 'self.' make capture semantics explicit'
seems me suggestion use self.
instead of usersarray.
because within closure, i'm given error if run way, *classname* not have member named 'append'
here code:
import uikit class searchusersregistrationviewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource { var userarray = [string]() @iboutlet var tableview: uitableview! override func viewdidload() { super.viewdidload() tableview.delegate = self tableview.datasource = self } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func loadparsedata(){ var query : pfquery = pfuser.query() query.findobjectsinbackgroundwithblock { (objects:[anyobject]!, error:nserror!) -> void in if error != nil{ println("\(objects.count) users listed") object in objects { userarray.append(object.userarray string) } } } } let textcellidentifier = "cell" func numberofsectionsintableview(tableview: uitableview) -> int { return 1 } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { //return usersarray.count } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier(textcellidentifier, forindexpath: indexpath) searchusersregistrationtableviewcell let row = indexpath.row //cell.userimage.image = uiimage(named: usersarray[row]) //cell.usernamelabel?.text = usersarray[row] return cell } }
the problem userarray nsarray. nsarray immutable, meaning can't changed. therefore doesn't have append function. want nsmutablearray, can changed , has addobject function.
var userarray:nsmutablearray = [] func loadparsedata(){ var query : pfquery = pfuser.query() query.findobjectsinbackgroundwithblock { (objects:[anyobject]!, error:nserror!) -> void in if error == nil { if let objects = objects { object in objects { self.userarray.addobject(object) } } self.tableview.reloaddata() } else { println("there error") } } }
also, because objects returned 'anyobject' have cast them pfusers @ point in order use them such. keep in mind
for getting user's username , displaying it
// put in cellforrowatindexpath
var user = userarray[indexpath.row] as! pfuser var username = user.username as! string cell.usernamelabel.text = username