解析和Swift 1.2问题 [英] Parse and Swift 1.2 issue

查看:83
本文介绍了解析和Swift 1.2问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码在Swift 1.1中运行良好...只是试图找出1.2中的更改以使其不兼容:

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)

    })
}

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

It gives me the error: Cannot invoke 'GetObjectInBackgroundWithId' with an argument list of type '(String, block: (PFObject!, NSError) -> Void)

有什么想法吗?谢谢!

推荐答案

现在,使用Swift 1.2时,您应该更加谨慎地使用可选内容.因此,在具有PFObjectNSError的闭包内部,请删除感叹号或添加问号以使其成为可选.

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)
            }
        }

    })
}

这篇关于解析和Swift 1.2问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆