Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了swift3 – 在Swift 3中设置URL资源值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我试图将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“跳转到定义”之外,我找不到任何文档

有没有人对setresourceValue(s)的变化有任何了解?

解决方法

NSFileAccessIntent.URL的当前声明是

public var url: URL { get }

这是一个只读属性.

由于Swift 3 URL是一个结构,因此您不能对从getter返回的不可变值@L_714_7@mutaTing方法.要修改URL,首先将其分配给var.

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

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

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。