admin管理员组

文章数量:1125588

I want to create a folder in the system's Documents folder like the application CASRER, but it fails。

public extension URL {
    static var userHome : URL   {
        URL(fileURLWithPath: userHomePath, isDirectory: true)
    }
    
    static var userHomePath : String   {
        let pw = getpwuid(getuid())

        if let home = pw?.pointee.pw_dir {
            return FileManager.default.string(withFileSystemRepresentation: home, length: Int(strlen(home)))
        }
        
        fatalError()
    }
}

        let homeDirectory = URL.userHome
        
        //Music
        let documentsDirectory = homeDirectory.appendingPathComponent("Documents")
        
        let newFolderName = "TEST"
        
        let newFolderURL = documentsDirectory.appendingPathComponent(newFolderName)
        
        if !FileManager.default.fileExists(atPath: newFolderURL.path) {
            do {
                try FileManager.default.createDirectory(at: newFolderURL, withIntermediateDirectories: true, attributes: nil)
                print("文件夹创建成功:\(newFolderURL.path)")
            } catch {
                print("创建文件夹失败: \(error)")
            }
        } else {
            print("文件夹已存在:\(newFolderURL.path)")
        }

Error Domain=NSCocoaErrorDomain Code=513 "你没有将文件“TEST”存储到文件夹“文稿”中的权限。" UserInfo={NSFilePath=/Users/peanut/Documents/TEST, NSURL=file:///Users/peanut/Documents/TEST, NSUnderlyingError=0x6000010aee50 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}} auto start register ok

本文标签: macosHow to create a folder in the system documentsStack Overflow