ios - How to Return list of anyObjects with swift? -


how return objects retrieved parse? i'm having problem returning either list of pfobject or anyobject. tried not return list , assign self.array within method.

i retrieve objects using query in getsteps() method:

var query = pfquery(classname:"intervaller") query.wherekey("namn", equalto:"elit")  query.findobjectsinbackgroundwithblock {     (objects: [anyobject]?, error: nserror?) -> void in 

i can use list of objects within same method without problem casting pfobject:

if let list = objects as? [pfobject] {        object in list {         let steps = object["steps"] as! int         var mystring = string(steps)          println(mystring)     } } 

to use objects in other methods should return list of pfobject or anyobject? have tried both without success, i.e. no objects assigned new list.

if try self.steps = objects! assign list var steps = [anyobject]() i'm not sure if objects being assigned new list. not values out in loop using code

self.getsteps() //calling method parse query run , self.steps assigned objects!    if let list = self.steps as? [pfobject] {     object in list {         let steps = object["steps"] as! int         var mystring = string(steps)         println(mystring)     }  } 

whats best approach , how cast , return/assign correctly?

func getsteps() {     var query = pfquery(classname:"intervaller")     query.wherekey("namn", equalto:"elit")      query.findobjectsinbackgroundwithblock {         (objects: [anyobject]?, error: nserror?) -> void in         self.steps = objects as? [pfobject]         // @ point, sometime after getsteps method has returned, self.steps valid     }     // @ point in code completion handler has not been called cannot return list. } 

for example:

self.getsteps() //calling method parse query runned , (self.steps not assigned before method returns.) // here self.steps not valid, since parse query running in background , completion block not have been executed yet. // following code best off being included in completion handler of parse query. if let list = self.steps as? [pfobject] {     object in list {         let steps = object["steps"] as! int         var mystring = string(steps)         println(mystring)     } } 

so either include code needs self.steps in completion handler in getsteps() method. or can make getsteps method asynchronous giving completion block call queries completion block:

func getsteps(completion: (objects: [anyobject]?, error: nserror?) -> void) {     var query = pfquery(classname:"intervaller")     query.wherekey("namn", equalto:"elit")      query.findobjectsinbackgroundwithblock {         (objects: [anyobject]?, error: nserror?) -> void in         self.steps = objects as? [pfobject]         // call getsteps method's completion handler         completion(objects: objects, error: error)     } } 

then can call:

self.getsteps() { (objects: [anyobject]?, error: nserror?) -> void in     if let list = self.steps as? [pfobject] {         object in list {             let steps = object["steps"] as! int             var mystring = string(steps)             println(mystring)         }     } } 

let me know if of needs clarifying.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -