ios - How to receive String Array from Parse Cloud Code in Swift? -
i've written parse cloud code function returns data database. see in "response" when println in xcode. looks it's wrapped in double optional!? i'm making wrong in if let
, in for
loop? how (unwrap) string array out of it?
my code in swift:
pfcloud.callfunctioninbackground("toptwo", withparameters: ["rating":5]) { (response: anyobject?, error: nserror?) -> void in if error == nil { println("successfully retrieved \(response!.count) scores.") println("here flower names: \(response)") if let objects = response as? [pfobject] { object in objects { println(object.objectid) } } } else { println("error: \(error!) \(error!.userinfo!)") } }
what see in console:
successfully retrieved 2 scores. here flower names: optional(( rose, "sunflower" ))
maybe there error in cloude code. here can see i've done:
parse.cloud.define("toptwo", function(request, response) { var query = new parse.query("flowers"); console.error("get flowers rating: " + request.params.rating); query.equalto("stars", request.params.rating); query.find({ success: function(results) { console.error("results: " + results); var list = []; (i = 0; < results.length; i++) { list[i] = results[i].get('flowername'); } console.error("flower name list: " + list); response.success(list); }, error: function() { response.error("lookup failed"); } }); });
and here parse logs:
results: [object object],[object object] flower name list: rose,sunflower
(i'm using xcode 6.3.2 - swift 1.2)
many in advance!
okay, solve on own.
object returned cloud code array. therefore casting nsarray
has made instead of casting [pfobject]
.
here working swift code:
pfcloud.callfunctioninbackground("daytopfive", withparameters: ["day":1]) { (response: anyobject?, error: nserror?) -> void in if error == nil { println("successfully retrieved \(response!.count) scores.") // working: let objects = response as! nsarray object in objects { println("a top flower is: \(object)") } } }
Comments
Post a Comment