Swift3 在Swift 3中设置URL资源值

Swift3 在Swift 3中设置URL资源值,swift3,Swift3,我正试图将苹果的示例转换为Swift 3,但我无法理解对URL的setResourceValue的更改 Apple(Swift 2)示例具有以下代码: // Coordinate reading on the source path and writing on the destination path to copy. let readIntent = NSFileAccessIntent.readingIntentWithURL(templateURL, options: []) let w

我正试图将苹果的示例转换为Swift 3,但我无法理解对
URL
setResourceValue
的更改

Apple(Swift 2)示例具有以下代码:

// Coordinate reading on the source path and writing on the destination path to copy.
let readIntent = NSFileAccessIntent.readingIntentWithURL(templateURL, options: [])
let writeIntent = NSFileAccessIntent.writingIntentWithURL(target, options: .ForReplacing)

NSFileCoordinator().coordinateAccessWithIntents([readIntent, writeIntent], queue: self.coordinationQueue) { error in
    if error != nil { return }
    do {
        try fileManager.copyItemAtURL(readIntent.URL, toURL: writeIntent.URL)    
        try writeIntent.URL.setResourceValue(true, forKey: NSURLHasHiddenExtensionKey)    
        NSOperationQueue.mainQueue().addOperationWithBlock {
            self.openDocumentAtURL(writeIntent.URL)
        }
    } catch {
        fatalError("Unexpected error during trivial file operations: \(error)")
    }
}
setResourceValue(value:forKey:)
似乎已被
setResourceValues()
替换,但我无法设置它。到目前为止,我得到的是:

let readIntent = NSFileAccessIntent.readingIntent(with: templateURL, options: [])
let writeIntent = NSFileAccessIntent.writingIntent(with: target, options: .forReplacing)

NSFileCoordinator().coordinate(with: [readIntent, writeIntent], queue: self.coordinationQueue) { error in
    if error != nil { return }                
    do {
        try fileManager.copyItem(at: readIntent.url, to: writeIntent.url)
        var resourceValues: URLResourceValues = URLResourceValues.init()
        resourceValues.hasHiddenExtension = true
        // *** Error on next line ***
        try writeIntent.url.setResourceValues(resourceValues)
        // Cannot use mutating member on immutable value: 'url' is a get-only property

        OperationQueue.main.addOperation {
            self.openDocumentAtURL(writeIntent.URL)
        }
    } catch {
        fatalError("Unexpected error during trivial file operations: \(error)")
    }      
}
除了Xcode“跳转到定义”之外,我找不到其他文档,它说

设置由给定资源键标识的资源值

此方法将新资源值写入备份存储。设置只读资源属性或设置资源不支持的资源属性的尝试将被忽略,不会被视为错误。此方法当前仅适用于文件系统资源的URL

URLResourceValues
跟踪已设置的属性。这些值是此函数用于 确定要写入的属性

public mutating func setResourceValues(uValues:URLResourceValues)抛出


是否有人对
setResourceValue
的更改有任何了解?

NSFileAccessIntent.URL的当前声明是

public var url: URL { get }
它是只读属性

由于swift3
URL
是一个
struct
,因此不能对getter返回的不可变值调用mutating方法。要修改URL,请首先将其分配给
var

var url = intent.URL
url.setResourceValues(...)

然后从修改后的URL创建一个新的意图。

接受的答案很有帮助,但在学习新内容时,工作代码甚至更好,我们假设错误在那里,而不是在旧内容中!你几乎是对的,但是
intent.url仍然是只读的,所以你的第三行行不通。但是这个想法是正确的-使用修改后的URL重新初始化意图有效-
intent=NSFileAccessIntent.writingcontent(使用:URL,options:.forreplacement)
。你想更新答案,还是我来更新?也许可以放置你自己的答案,我将删除此答案:)我花了一段时间才找到当前文档。请确保定义url使用var不允许