ios - Converting PFObject (Parse) into JSON in Swift? -
is there way convert pfobject parse json? saved json, when try load i'm getting [anyobject] back. casting json won't work:
class func loadpeople() -> [string : person] { var peopledictionary: [string : person] = [:] let query = pfquery(classname: "userpeeps") query.findobjectsinbackgroundwithblock { (objects, error) -> void in if error == nil { //this returns first entry, how them all? if let peoplefromparse = objects?.first?.objectforkey("userpeeps") as? json { name in peoplefromparse.keys { if let personjson = peoplefromparse[name] as? json, let person = person(json: personjson) { peopledictionary[name] = person } } }
below save function, works , saves json parse want:
class datamanager { typealias json = [string: anyobject] class func savepeople(people: [string : person]) { var peopledictionary = people var peoplejson: json = [:] name in peopledictionary.keys { peoplejson[name] = peopledictionary[name]!.tojson() } let userpeeps = pfobject(classname: "userpeeps") userpeeps.setobject(peoplejson, forkey: "userpeeps") userpeeps.saveinbackgroundwithblock { (succeeded, error) -> void in if succeeded { println("object uploaded") } else { println("error: \(error) \(error!.userinfo!)") } } }
so answer (as paulw11 points out above) "objects" sort of wrapper real data, necessary iterate through array , store each value json:
var peopledictionary: [string : person] = [:] //1 load dictionary of json key people parse let query = pfquery(classname: "userpeeps") query.findobjectsinbackgroundwithblock { (objects, error) -> void in if error == nil { if let unwrappedobjects = objects { object in unwrappedobjects { if let peoplefromparse = object as? json { name in peoplefromparse.keys { if let personjson = peoplefromparse[name] as? json, let person = person(json: personjson) { peopledictionary[name] = person } } } } }
Comments
Post a Comment