【问题标题】:Swift Optionals ErrorSwift 选项错误
【发布时间】:2014-11-12 19:43:01
【问题描述】:

我收到以下代码错误。它来自 data.writeToFile。我试过 photoPath、photoPath!、photoPath?,但它总是出错。错误是:getData 调用中的 extraArgument(这不是错误,因为当我注释掉 data.writeToFile 时它工作正常)。

let file = object.objectForKey("image") as PFFile
let photoPath = NSURL.fileURLWithPath(NSTemporaryDirectory())?.URLByAppendingPathComponent("object", isDirectory: true).URLByAppendingPathExtension("jpg")

file.getDataInBackgroundWithBlock({ (data: NSData!, error: NSError!) -> Void in
    data.writeToFile(photoPath!, atomically: true)        
}, progressBlock: { (progress: Int32) -> Void in
    println(progress)
})

【问题讨论】:

    标签: swift parse-platform


    【解决方案1】:

    听起来 Swift 试图捕获 writeToFile() 的返回值并从闭包中隐式返回它。您只需明确说明此处返回的内容。

    file.getDataInBackgroundWithBlock({ (data: NSData!, error: NSError!) -> Void in
        data.writeToFile(photoPath!, atomically: true)
        return        
    }, progressBlock: { (progress: Int32) -> Void in
        println(progress)
    })
    

    【讨论】:

    • 非常感谢!这就是问题所在。
    • 那么这里返回的是什么? getDataInBackgroundWithBlock 似乎期待结果块(根据parse.com/docs/osx/api/Classes/PFFile.html#//api/name/…)?
    • 刚刚做了。你必须在提出问题后等待几分钟才能接受,否则我会立即接受。
    • 所以我知道(data: NSData!, error: NSError!) 如何满足结果块,我想我看不到writeToFile 返回的内容会影响in。再说一次,我已经很久没有阅读 Swift API
    • 啊它返回一个布尔值。出于某种原因,我还认为 void 可以接受任何返回类型,但忽略所有值,类似于 Nulls 函数的方式 - 显然我已经很远了(自从我'看过 C/C++/C#/Obj-C)。谢谢,0x7fffffff。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    相关资源
    最近更新 更多