【问题标题】:How to exclude file from backup in Swift?如何在 Swift 中从备份中排除文件?
【发布时间】:2015-12-16 11:21:49
【问题描述】:

当我使用 filePath 作为字符串时,我在 Swift 1.2 中表现出色。现在 Swift 2 希望我们都使用 URL 路径,即使我正在阅读他们的文档,我也无法让它工作。

我有;

var fileName = "myRespondusCSV.csv"

let fileManager = NSFileManager.defaultManager()

let documentsURL = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)


if let documentPath: NSURL = documentsURL.first as NSURL! {

    filePath = documentPath.URLByAppendingPathComponent(fileName)
        print(filePath)        
    } else {
        fileManager.createFileAtPath(filePath!.path!,
                                     contents: ("" as String).dataUsingEncoding(NSUTF8StringEncoding)!,
                                     attributes:nil)
        print("file has been created")
    }
}

func excludeFileFromBackup() {

    var error:NSError?
    //var fileToExcludeh = NSURL.fileReferenceURL(filePath!)

    var fileToExcludeh = fileURLWithPath(filePath)

    let success = fileToExcludeh.setResourceValue(true, forKey: NSURLIsExcludedFromBackupKey, error: &error)
}

我收到“使用未解析的标识符“fileURLWithPath”!

我应该使用绝对 URL 路径吗?

【问题讨论】:

  • fileURLWithPath 不是(全局)函数,而是 NSURL 的方法。 SO如何使用该方法的示例有很多,最近的一个是stackoverflow.com/questions/32665508/…
  • 注释代码`//var fileToExcludeh = NSURL.fileReferenceURL(filePath!)` 似乎是正确的,那你为什么要评论呢?
  • 这里的示例stackoverflow.com/questions/32665508 cannot-invoke-initializer-for-type-nsurl-with-an-argument-list-of-type-fileudoes 没有(到目前为止)有答案。 Swift 2 删除了 stringByAppendingPathComponent,这意味着我需要将路径创建为 URL。回答 Midhun MP;注释代码抛出另一个错误'函数值被用作属性;添加()来调用'。在我的代码“fileToExcludeh”中没有具有此名称的函数。

标签: ios swift nsurl


【解决方案1】:

这应该可以工作

        do {
            try filePath.setResourceValue(true, forKey: NSURLIsExcludedFromBackupKey)
        } catch _{
            print("Failed")
        }

【讨论】:

  • 谢谢!刚刚回到这个;你的解决方案更好!
  • 可以写成一行:试试? filePath.setResourceValue(true, forKey: NSURLIsExcludedFromBackupKey)
  • 我收到Cannot convert value of type 'Bool' to expected argument type 'AnyObject?'
【解决方案2】:

Swift 4,如果有人需要的话:

var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try? fileUrl.setResourceValues(resourceValues)

【讨论】:

  • 在 Swift 4 中对我来说很好
【解决方案3】:

这适用于 Swift 3。 URL 必须是 var

let urls:[URL] = FileManager.default.urls(for:FileManager.SearchPathDirectory.documentDirectory, in:FileManager.SearchPathDomainMask.userDomainMask)
let appDirectory:URL = urls.last!
var fileUrl:URL = appDirectory.appendingPathComponent("myFile")

var resourceValues:URLResourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true

do
{
    try fileUrl.setResourceValues(resourceValues)
}
catch let error
{
    print(error.localizedDescription)
}

【讨论】:

  • 给定的fileUrl 是否已修改,或者是否有其他技术原因使其成为 var?我查看了源代码 (github.com/apple/swift-corelibs-foundation/blob/master/…),但无法确定。
  • @Klaas URL 在您修改其资源值时必须是可变的。
  • 啊,有道理。我确实假设那些没有缓存并直接写入文件系统。
【解决方案4】:

这是我在 Swift 3

上的解决方案
func addSkipBackupAttributeToItemAtURL(URL:NSURL) -> Bool {
    var success: Bool

    do {
        try URL.setResourceValue(true, forKey:URLResourceKey.isExcludedFromBackupKey)
        success = true
    } catch let error as NSError {
        success = false
        print("Error excluding \(URL.lastPathComponent!) from backup \(error)");
    }
    return success
}

【讨论】:

    【解决方案5】:

    在 swift 5 中为我运行的代码是...

    var file: URL = <# your URL file #>
    do {
       var res = URLResourceValues()
       res.isExcludedFromBackup = true
       try file.setResourceValues(res)
    } catch {
       print(error)
    }
    

    当然你可以把它放在函数或扩展中。

    【讨论】:

      猜你喜欢
      • 2012-08-19
      • 2011-03-18
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多