【问题标题】:I'm getting an "Extra argument" error trying to use the coordinateWritingItemAtURL() method of NSFileCoordinator我在尝试使用 NSFileCoordinator 的 coordinateWritingItemAtURL() 方法时遇到“额外参数”错误
【发布时间】:2014-12-16 16:41:42
【问题描述】:

错误是:调用中的额外参数“writingItemAtURL”

我正在为 iOS 开发 Swift
我在 Yosemite 上使用 Xcode 6.1.1
我尝试删除 DerivedData 并重新启动 xcode,但没有帮助。

这里有一些说明问题的操场代码:

let movingOption = NSFileCoordinatorWritingOptions.ForMoving
let replacingOption = NSFileCoordinatorWritingOptions.ForReplacing
var testURL1 = NSURL(fileURLWithPath: "file1")
var testURL2 = NSURL(fileURLWithPath: "file2")
var error1: NSErrorPointer?
var error2: NSErrorPointer?
let fc = NSFileCoordinator(filePresenter: nil)
var result = false

fc.coordinateWritingItemAtURL(testURL1, options: movingOption, writingItemAtURL: testURL2, options: replacingOption, error: &error1, byAccessor: { (newURL1: NSURL, newURL2: NSURL) in
    let fm = NSFileManager.defaultManager()
    result = fm.moveItemAtURL(newURL1, toURL: newURL2, error: error2)
    if !result {
        println("DEBUG: Failed to move file \(moveError?.localizedDescription)")
    }
})

【问题讨论】:

    标签: ios xcode swift


    【解决方案1】:

    将最后一行替换为:

    fc.coordinateWritingItemAtURL(testURL1, options: movingOption, writingItemAtURL: testURL2, options: replacingOption, error: &error1) { (newURL1: NSURL, newURL2: NSURL) -> Void in
        let fm = NSFileManager.defaultManager()
        result = fm.moveItemAtURL(newURL1, toURL: newURL2, error: error2)
        if !result {
            println("DEBUG: Failed to move file \(moveError?.localizedDescription)")
    
        }
    }
    

    【讨论】:

    • 使用该行,错误现在是“Extra argument 'options' in call”
    • 抱歉,昨天在操场上没有显示该错误,但是今天早上打开它时,我看到了同样的错误。啊。
    【解决方案2】:

    我发现了我的愚蠢错误。参数列表中的 URL 需要解包,因此工作代码如下所示:

    let movingOption = NSFileCoordinatorWritingOptions.ForMoving
    let replacingOption = NSFileCoordinatorWritingOptions.ForReplacing
    var testURL1 = NSURL(fileURLWithPath: "file1")!
    var testURL2 = NSURL(fileURLWithPath: "file2")!
    var error1: NSError?
    var error2: NSError?
    let fc = NSFileCoordinator(filePresenter: nil)
    var result = false
    
    fc.coordinateWritingItemAtURL(testURL1, options: movingOption, writingItemAtURL: testURL2, options: replacingOption, error: &error1, byAccessor: { (newURL1: NSURL!, newURL2: NSURL!) in
        let fm = NSFileManager.defaultManager()
        result = fm.moveItemAtURL(newURL1, toURL: newURL2, error: &error2)
        if !result {
            println("DEBUG: Failed to move file \(error2?.localizedDescription)")
        }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 2021-01-11
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-07
      相关资源
      最近更新 更多