1
votes

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

1

1 Answers

2
votes

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