This code worked fine in Swift 1.1 ... just trying to figure out what's changed in 1.2 to make it incompatible:
@IBAction func load_click(sender: AnyObject) {
var query = PFQuery(className: "myClass")
query.getObjectInBackgroundWithId("MPSVivtvJR", block: { (object:PFObject!, error: NSError) -> Void in
let theName = object["name"] as String
let theAge = object["age"] as Int?
println(theName)
println(theAge)
})
}
It gives me the error: Cannot invoke 'GetObjectInBackgroundWithId' with an argument list of type '(String, block: (PFObject!, NSError) -> Void)
Any ideas? Thanks!
Now with Swift 1.2 you are supposed to be more careful with unwrapping optionals. So inside the closure where you have PFObject
and NSError
, either remove the exclamation marks or add a question mark to make it optional.
Then, unwrap your object more safely. Try as follows:
// You can create this in a separate file where you save your models
struct myUser {
let name: String?
let age: Int?
}
// Now this in the view controller
@IBAction func load_click(sender: AnyObject) {
var query = PFQuery(className: "myClass")
query.getObjectInBackgroundWithId("MPSVivtvJR", block: {
(object:PFObject!, error: NSError?) -> Void in
if let thisName = object["name"] as? String{
if let thisAge = object["age"] as? Int{
let user = myUser(name: thisName, age: thisAge)
println(user)
}
}
})
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments