没有那个文件或目录,NSFilePath=~/Library

No such file or directory, NSFilePath=~/Library

当我 运行 下面的代码片段

时,会抛出下面引用的错误
fileManager.contentsOfDirectoryAtPath(libraryPath)

错误

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 )}

这是我使用的完整代码片段,以防万一有人想尝试

   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)
        }

    }

如果有人能澄清为什么会发生此错误并提出解决方案,那就太好了

在您的代码中:

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

设置权限true:

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