MacOS 开发收录

MacOS 开发(十三) : 文件保存

2020-04-03  本文已影响0人  SoaringHeart
WechatIMG326.jpeg
    @objc func saveFile(_ data: NSData, name: String, allowedFileTypes: [String] = ["jpg","png"]) {
        let panel = NSSavePanel()
        panel.title = "保存文件"
        panel.message = "请选择文件保存地址"
        panel.directoryURL = URL(string: "\(NSHomeDirectory())/Downloads")
        panel.nameFieldStringValue = name
        panel.allowsOtherFileTypes = true
        panel.allowedFileTypes = allowedFileTypes
        panel.isExtensionHidden = false
        panel.canCreateDirectories = true
        
        panel.begin { (response) in
            if response == .OK {
                if let path = panel.url?.path {
                    data.write(toFile: path, atomically: true)
                }
            }
        }
    }
上一篇下一篇

猜你喜欢

热点阅读