Search code examples
iosswiftcocoa-touchnsfilemanager

How to access created Directory? - NSCocoaErrorDomain Code=257


I am trying to create a new directory and put files into it. However, I'm getting

Error Domain=NSCocoaErrorDomain Code=257 "The file “Offline” couldn’t be opened because you don’t have permission to view it."

I am able to create this new directory and the file exists.

let offlinePath = fileDirectory.appendingPathComponent("Offline")
try? fileManager.createDirectory(at: offlinePath, withIntermediateDirectories: true, attributes: nil)

files.forEach { file in
    if let localUrl = file.localUrl {
      do {
          try fileManager.moveItem(at: localUrl, to: offlinePath)
          file.localUrl = offlinePath
      } catch {
          print(error)
      }
 }

Solution

  • You cannot move a file to a directory without appending the file name

    do {
        let fileName = localUrl.lastPathComponent
        let offlineURL = offlinePath.appendingPathComponent(fileName)
        try fileManager.moveItem(at: localUrl, to: offlineURL)
        file.localUrl = offlineURL
    } ...