尝试使用NSFileCoordinator的coordinateWritingItemAtURL()方法时出现“额外参数”错误

时间:2014-12-16 16:41:43

标签: ios xcode swift

错误是:额外的争论' 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)")
    }
})

2 个答案:

答案 0 :(得分:0)

用这个替换最后一行:

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

    }
}

答案 1 :(得分:0)

我发现了我的愚蠢错误。参数列表中的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)")
    }
})