这段代码在Swift 1.1中运行良好...只是试图找出1.2中所做的更改以使其不兼容:
@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)
})
}
它给了我错误:无法使用类型为'(String,block:(PFObject !, NSError)-> Void)的参数列表调用'GetObjectInBackgroundWithId'
有任何想法吗?谢谢!
现在,使用Swift 1.2时,您应该更加谨慎地使用可选选项。因此,在具有PFObject
和的闭包中NSError
,删除感叹号或添加问号以使其成为可选。
然后,更安全地解开对象。尝试如下:
// 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)
}
}
})
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句