gpt4 book ai didi

ios - 无法设置 NSURLIsExcludedFromBackupKey 属性

转载 作者:行者123 更新时间:2023-11-29 01:24:07 26 4
gpt4 key购买 nike

我正在尝试设置属性 NSURLIsExcludedFromBackupKey 但找不到文件夹(或文件,如果我尝试分别对每个文件执行此操作)异常。

代码如下:

override func viewDidLoad() {

var paths:[AnyObject] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
var documentsDirectory = paths[0] as? String
documentsRoot = documentsDirectory! + "/"

var root = NSURL(string: documentsRoot!)

addSkipBackupAttributeToItemAtURL(root!)
}

func addSkipBackupAttributeToItemAtURL(URL: NSURL) -> Void {

var filepath = NSURL(fileURLWithPath: "\(URL)")

let fileManager = NSFileManager.defaultManager()
assert(fileManager.fileExistsAtPath(URL.absoluteString))

var error:NSError?

do {
try filepath.setResourceValue(NSNumber(bool: true), forKey: NSURLIsExcludedFromBackupKey)
}
catch let error as NSError {
print("Error excluding \(filepath.lastPathComponent) from backup \(error)")
}

return
}

当我运行代码时,我得到了这个输出

Error excluding Optional(" ... cuments") from backup Error Domain=NSCocoaErrorDomain Code=4 "The file “ ... cuments” doesn’t exist." UserInfo={NSURL=file:///Users/me/Library/Developer/CoreSimulator/Devices/D1FC9BFB-4F95-440D-A3C5-ED1C0665A610/data/Containers/Data/Application/%20...%20cuments/, NSFilePath=/Users/me/Library/Developer/CoreSimulator/Devices/D1FC9BFB-4F95-440D-A3C5-ED1C0665A610/data/Containers/Data/Application/ ... cuments, NSUnderlyingError=0x7f86b2c80850 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

我做错了什么?

最佳答案

看起来你在这里使用了 URL 的文本描述:

var filepath = NSURL(fileURLWithPath: "\(URL)") // \(URL) is a string that contains not just the path, but also the optionality of the variable

而你应该使用 path NSURL 上的属性。

关于ios - 无法设置 NSURLIsExcludedFromBackupKey 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34222515/

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