2017-01-23 15 views
0

Если я пытаюсь удалить файл с помощью класса FileManager, я получаю сообщение об ошибке.Swift 3.0 и iOS: Как установить привилегии удаления файла в Swift

Функция ниже возвращает истинное, а это значит, что мне нужно установить УДАЛИТЬ привилегии атрибуты. Однако I 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!)") 
      } 
     } 
    } 
} 
+1

http://stackoverflow.com/questions/40642217/uiimagecontentsoffile-returning-nil-despite-file-existing-in-caches-directory/40643037?s=1|0.8206#40643037 –

+1

Спасибо! Это помогло решить – mm24

ответ

1

Существует распространенное заблуждение:

В файловой системе вы должны вызвать path на URL, чтобы получить путь

fileManager.isDeletableFile(atPath: fileAsNSURL.path) 

absoluteString возвращает строки (проценты экранированных строк) ентация в URL, начиная со схемой (http://, file://)


Например у вас есть URL (не используйте NSURL в Swift 3):

let url = URL(fileURLWithPath:"/Users/myUser/Application Support") 
  • url.path возвращается "/Users/myUser/Application Support"
  • url.absoluteString"file:///Users/myUser/Application%20Support"