parse.com – Parse和Swift 1.2问题
发布时间:2020-12-14 05:42:33 所属栏目:百科 来源:网络整理
导读:这段代码在Swift 1.1中运行良好…只是试图找出1.2中的变化使其不兼容: @IBAction func load_click(sender: AnyObject) { var query = PFQuery(className: "myClass") query.getObjectInBackgroundWithId("MPSVivtvJR",block: { (object:PFObject!,error: NS
这段代码在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) } } }) } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |