screenshot.png
@objc func pickFile(_ allowedFileTypes: [String] = ["jpg","png"], allowsMultipleSelection: Bool = false) {
let panel = NSOpenPanel()
// prompt : 修改默认打开按钮的文字
// panel.prompt = "打开"
panel.message = "不要随便选择文件"
// 设置文件打开的类型: 默认为空,表示可以选择任意类型的文件
panel.allowedFileTypes = allowedFileTypes
panel.allowsMultipleSelection = allowsMultipleSelection
// panel.canCreateDirectories = true
// 设置默认打开的文件路径
let pathUrl = URL(string: "\(NSHomeDirectory())/Downloads")
panel.directoryURL = pathUrl
// openpanel.allowsMultipleSelection = false
panel.delegate = self
// 显示openpanel
panel.begin { (response) in
if response == NSApplication.ModalResponse.OK {
for u in panel.urls {
print(u.absoluteString)
}
}
}
}
网友评论