作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试弄清楚我应该如何使用 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") }
}
我得到的错误是
我使用上面的代码将文件从备份到 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/
我正在尝试弄清楚我应该如何使用 Swift 3 继续修复下面的代码。 let paths = NSSearchPathForDirectoriesInDomains(.libraryDirectory
我是一名优秀的程序员,十分优秀!