【问题标题】:Cannot set NSURLIsExcludedFromBackupKey property无法设置 NSURLIsExcludedFromBackupKey 属性
【发布时间】:2016-03-17 07:20:34
【问题描述】:

我正在尝试设置属性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
}

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

从备份错误中排除 Optional(" ... cuments") 时出错 Domain=NSCocoaErrorDomain Code=4 "文件“... cuments”没有 存在。” 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 {错误 Domain=NSPOSIXErrorDomain Code=2 "没有这样的文件或目录"}}

我做错了什么?

【问题讨论】:

    标签: ios swift nsfilemanager


    【解决方案1】:

    您似乎在这里使用了 URL 的文本描述:

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

    而您应该在 NSURL 上使用 path 属性。

    【讨论】:

      猜你喜欢
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 2022-08-24
      • 1970-01-01
      相关资源
      最近更新 更多