parse.com – Parse和Swift 1.2问题

前端之家收集整理的这篇文章主要介绍了parse.com – Parse和Swift 1.2问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这段代码在Swift 1.1中运行良好…只是试图找出1.2中的变化使其不兼容:
  1. @IBAction func load_click(sender: AnyObject) {
  2.  
  3. var query = PFQuery(className: "myClass")
  4. query.getObjectInBackgroundWithId("MPSVivtvJR",block: { (object:PFObject!,error: NSError) -> Void in
  5.  
  6. let theName = object["name"] as String
  7. let theAge = object["age"] as Int?
  8.  
  9. println(theName)
  10. println(theAge)
  11.  
  12. })
  13. }

它给了我错误:无法使用类型'(String,block:(PFObject!,NSError) – > Void)的参数列表调用’GetObjectInBackgroundWithId’

有任何想法吗?谢谢!

现在使用Swift 1.2,你应该更加小心打开选项.因此,在具有PFObject和NSError的闭包内,要么删除感叹号,要么添加问号以使其成为可选项.

然后,更安全地打开您的物体.尝试如下:

  1. // You can create this in a separate file where you save your models
  2.  
  3. struct myUser {
  4. let name: String?
  5. let age: Int?
  6. }
  7.  
  8. // Now this in the view controller
  9.  
  10. @IBAction func load_click(sender: AnyObject) {
  11. var query = PFQuery(className: "myClass")
  12. query.getObjectInBackgroundWithId("MPSVivtvJR",block: {
  13. (object:PFObject!,error: NSError?) -> Void in
  14.  
  15. if let thisName = object["name"] as? String{
  16. if let thisAge = object["age"] as? Int{
  17. let user = myUser(name: thisName,age: thisAge)
  18. println(user)
  19. }
  20. }
  21.  
  22. })
  23. }

猜你在找的Swift相关文章