gpt4 book ai didi

ios - URLResourceValue 和 setResourceValues Swift 3

转载 作者:可可西里 更新时间:2023-11-01 03:02:02 28 4
gpt4 key购买 nike

我正在尝试弄清楚我应该如何使用 Swift 3 继续修复下面的代码。

let paths = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true) as NSArray

for path in paths {
let dir = path as! String
print("the paths are \(path)")
let urlToExclude = NSURL.fileURL(withPath: dir)
do {

try urlToExclude.setResourceValue(NSNumber(value: true), forKey: URLResourceKey.isExcludedFromBackupKey)

} catch { print("failed to set resource value") }
}

我得到的错误是 this

我使用上面的代码将文件从备份到 iCloud 中排除,它曾经在以前版本的 Swift 上运行良好,但在更新到 Xcode 8 后我就卡住了。

如果有任何帮助或建议,我将不胜感激。

最佳答案

Xcode 建议“改用 struct URLResourceValues 和 URL.setResourceValues(_:)”提示您编写如下内容:

let paths = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)

for dir in paths {
print("the paths are \(dir)")
var urlToExclude = URL(fileURLWithPath: dir)
do {

var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try urlToExclude.setResourceValues(resourceValues)

} catch { print("failed to set resource value") }
}

请尝试。

注意

请注意,文件 URL 必须是一个 var - 如果它是一个 let 你会收到奇怪的错误消息(见评论)。

关于ios - URLResourceValue 和 setResourceValues Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39630532/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com