2017-01-23 7 views
0

FileManagerクラスを使用してファイルを削除しようとすると、エラーが発生します。Swift 3.0とiOS:Swiftでファイルの削除権限を設定する方法

以下の関数は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!)") 
      } 
     } 
    } 
} 
+1

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

+1

ありがとうございました!それは解決に役立った – mm24

答えて

1

共通の誤解があります:

ファイルシステムで使用すると、パスに

fileManager.isDeletableFile(atPath: fileAsNSURL.path) 
を取得するために、URLに pathを呼び出す必要があり、A

absoluteStringは、(パーセントエスケープされた)文字列represを返しますスキーム(http://file://)で始まるURLのentation例えば


あなたは(スウィフト3にNSURLを使用しない)URLを持っている:

let url = URL(fileURLWithPath:"/Users/myUser/Application Support") 
  • url.path戻り"/Users/myUser/Application Support"
  • url.absoluteString返信"file:///Users/myUser/Application%20Support"
関連する問題