How to exclude file from backup with Swift 2?

∥☆過路亽.° 提交于 2019-12-07 10:10:36

问题


I had this working great with Swift 1.2 as I used the filePath as a string. Now Swift 2 wants us all to use URL paths I can't get this to work even though i'm reading through their docs.

I have;

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)
}

I'm getting a 'Use of unresolved identifier 'fileURLWithPath'!

Should I be using an absolute URL path?


回答1:


This should work

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



回答2:


Swift 4 if anyone needs it:

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



回答3:


This is working for Swift 3. URL must be a 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)
}



回答4:


Here is my resolution for it on 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:


The code that runs for me in swift 5 is...

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

Of course you can put this in a function or an extension.



来源:https://stackoverflow.com/questions/32666765/how-to-exclude-file-from-backup-with-swift-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!