在 Swift 3 中设置 URL 资源值

Setting URL resource values in Swift 3

我正在尝试将 Apple 的 "ShapeEdit" 示例转换为 Swift 3,但我无法理解对 URLsetResourceValue 的更改。

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 "jump to definition",我找不到其他文档,它说

Sets the resource value identified by a given resource key.

This method writes the new resource values out to the backing store. Attempts to set a read-only resource property or to set a resource property not supported by the resource are ignored and are not considered errors. This method is currently applicable only to URLs for file system resources.

URLResourceValues keeps track of which of its properties have been set. Those values are the ones used by this function to determine which properties to write.

public mutating func setResourceValues(_ values: URLResourceValues) throws

有没有人了解 setResourceValue(s) 的变化?

NSFileAccessIntent.URL的当前声明是

public var url: URL { get }

这是一个只读 属性。

由于 Swift 3 URLstruct,因此您不能对从 getter 返回的不可变值调用变异方法。要修改 URL 首先将其分配给 var.

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

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

接受的答案很有帮助,但工作代码更好

do {
    var resourceValues = URLResourceValues()
    resourceValues.isExcludedFromBackup = true
    try fileUrl.setResourceValues(resourceValues)
} catch _{
}

如@Sulthan 所述,fileURL 必须可变。