我在 Swift 1.2 中工作得很好,因为我使用 filePath 作为字符串。现在 Swift 2 希望我们所有人都使用 URL 路径,尽管我正在阅读他们的文档,但我无法让它工作。
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)
}
我收到“使用未解析的标识符‘fileURLWithPath’!”
我应该使用绝对 URL 路径吗?
Swift 4 如果有人需要的话:
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try? fileUrl.setResourceValues(resourceValues)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)