0

I've written a Parse cloud code function which returns some data from the database. I see those in the "response" when I do a println in XCode. It looks like it's wrapped in a double optional!? What I'm making wrong in the if let and in the for loop? How do I get (unwrap) a 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 are the flower names: \(response)")

            if let objects = response as? [PFObject] {
                for object in objects {
                    println(object.objectId)
                }
            }
        } else {
            println("Error: \(error!) \(error!.userInfo!)")
        }
    }  

What I see in the console:

Successfully retrieved 2 scores.
Here are the flower names: Optional((
    rose,
    "sunflower"
))

Maybe there is also an error in my cloude code. Here you can see what I've done:

Parse.Cloud.define("TopTwo", function(request, response) {
    var query = new Parse.Query("Flowers");
    console.error("Get flowers with the rating: " + request.params.rating);
    query.equalTo("stars", request.params.rating);
    query.find({
        success: function(results) {
            console.error("Results: " + results);

            var list = [];
            for (i = 0; i < 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 the parse logs:

Results: [object Object],[object Object]
Flower name list: rose,sunflower

(I'm using XCode 6.3.2 - Swift 1.2)
Many thanks in advance!

1 Answer 1

4

Okay, I could solve it on my own.
The object which is returned from cloud code is already an Array. Therefore a casting into NSArray has to be made instead of a casting into [PFObject].

Here is the working Swift code:

PFCloud.callFunctionInBackground("dayTopFive", withParameters: ["day":1]) {
        (response: AnyObject?, error: NSError?) -> Void in

        if error == nil {
            println("Successfully retrieved \(response!.count) scores.")

            // This is working: 
            let objects = response as! NSArray
            for object in objects {
                println("A top flower is: \(object)")       
            }
        }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This works for me when I needed to return list of relations from user

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.