Reputation: 255
I am building an app that will handle voting for a talent show. I want to run a query that runs against a Parse database, and return the "Total". Here's what I have:
var query = PFQuery(className:"points")
query.whereKey("Act", equalTo:"Act 1")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil
{
// The find succeeded.
var test = objects["Total"]
println(test)
// Do something with the found objects
} else {
// Log details of the failure
println("nope")
}
}
However when I run it, the console prints "Optional 3". Any advice?
Upvotes: 0
Views: 505
Reputation: 72750
Querying a dictionary always returns an optional, to handle the case when a key doesn't exist.
What you can do is apply the forced unwrapping operator !
:
var test = objects["Total"]!
but that generates a runtime exception if the key is not found. A better way is using optional binding:
if let test = objects["Total"] {
println(test)
}
The difference is that it is safer, and the if
branch is executed only if objects["Total"]
is not nil
Upvotes: 1