1

The below quoted error gets thrown when I run the following code snippet

fileManager.contentsOfDirectoryAtPath(libraryPath)

Error

Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x17585bf0 {NSUnderlyingError=0x175706b0 "The operation couldn’t be completed. No such file or directory", NSFilePath=~/Library, NSUserStringVariant=( Folder )}

Here is the complete code snippet that I used just in case if anybody wants to try

   func listLibDir(){

        let libraryPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, false).first!

        let fileManager: NSFileManager = NSFileManager.defaultManager()

        do{
            var directoryContent:Array<String> = try fileManager.contentsOfDirectoryAtPath(libraryPath)

            for fileName: String in directoryContent {
                print("library:\(fileName)")
            }
        }
        catch{
            print(error)
        }

    }

If somebody can clarify why this error happens and suggest a solution then it'd be great

Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241

1 Answers1

2

In your code:

let libraryPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, false).first!

Set the permission to true:

let libraryPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true).first
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Ramesh_T
  • 1,251
  • 8
  • 19
  • To add a bit of the "why" part.... Tilde expansion is usually done by the command line shell, so if you were passing the result to an external command or just displaying the result, `false` would be fine. The file manager routines don't do the expansion automatically. – Phillip Mills Jan 22 '16 at 14:34