swift3 – 在Swift 3中设置URL资源值

swift3 – 在Swift 3中设置URL资源值,第1张

概述我试图将Apple的 “ShapeEdit”示例转换为Swift 3,我无法理解对URL的setResourceValue的更改. Apple(Swift 2)示例包含以下代码: // Coordinate reading on the source path and writing on the destination path to copy.let readIntent = NSFileA 我试图将Apple的 “ShapeEdit”示例转换为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,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“跳转到定义”之外,我找不到任何文档

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 URL是一个结构,因此您不能对从getter返回的不可变值调用mutating方法.要修改URL,首先将其分配给var.

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

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

总结

以上是内存溢出为你收集整理的swift3 – 在Swift 3中设置URL资源值全部内容,希望文章能够帮你解决swift3 – 在Swift 3中设置URL资源值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1001597.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存