gpt4 book ai didi

ios - swift 3.0 和 iOS : How to set the delete privileges of a file in Swift

转载 作者:行者123 更新时间:2023-11-29 00:31:05 26 4
gpt4 key购买 nike

如果我尝试使用 FileManager 类删除文件,我会收到错误消息。

下面的函数返回true,这意味着我需要设置删除权限属性。然而我haven't找到了一个关于如何做的例子。

func isDeletableFile(atPath path: String) -> Bool

有什么帮助吗?

代码:

func fileManager(_ fileManager: FileManager, shouldRemoveItemAtPath path: String) -> Bool {
// print("Should remove invoked for path \(path)")
return true
}

func fileManager(_ fileManager: FileManager, shouldProceedAfterError error: Error, removingItemAt URL: URL) -> Bool {
//print("Should process")
return true
}

func deleteAllFiles(subPath : String) {
var url = Bundle.main.bundleURL
url = url.appendingPathComponent(subPath)

let fileManager = FileManager.default
fileManager.delegate = self

if let enumerator = fileManager.enumerator(at: url, includingPropertiesForKeys: nil) {
for file in enumerator {
let fileAsNSURL = file as! NSURL
print("")
print("Deleting: \(fileAsNSURL.absoluteString!)")
print("")

do {
// I would like to set the deletable permissions before checking this..
if (fileManager.isDeletableFile(atPath: fileAsNSURL.absoluteString!)){
try fileManager.removeItem(atPath: fileAsNSURL.absoluteString!)
}
else{
print("its not deletable")
}
}
catch let error {
print("file-delete-error:\n\(error) for path \(fileAsNSURL.absoluteString!)")
}
}
}
}

最佳答案

有一个普遍的误解:

在文件系统中,您必须在 URL 上调用 path 来获取路径

fileManager.isDeletableFile(atPath: fileAsNSURL.path)

absoluteString 返回以方案 (http://, file://) 开头的 URL 的(转义百分比)字符串表示)


例如你有一个 URL(不要在 Swift 3 中使用 NSURL):

let url = URL(fileURLWithPath:"/Users/myUser/Application Support")
  • url.path 返回 "/Users/myUser/Application Support"
  • url.absoluteString 返回 "file:///Users/myUser/Application%20Support"

关于ios - swift 3.0 和 iOS : How to set the delete privileges of a file in Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41809537/

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