// // ContentView.swift // fileModel // // Created by 陈春杨 on 2022/2/16. // import SwiftUI struct ContentView: View { var body: some View { Button { let path = loadFile() } label: { HStack { Image("upload").resizable().frame(width: 15, height: 15) Text("上传文件") .foregroundColor(Color.white) .font(Font.system(size: 14)) }.frame(width: 106, height: 32) .background(Color.red) }.buttonStyle(PlainButtonStyle()) .cornerRadius(4) .padding(EdgeInsets(top: 29, leading: 0, bottom: 29, trailing: 0)) } // 文件选择 func loadFile(isFolder: Bool = false, openFinder: String = "", fileTypes: [String] = [String]()) -> String { let dialog = NSOpenPanel() dialog.title = "选择文件" dialog.showsResizeIndicator = true dialog.showsHiddenFiles = false dialog.allowsMultipleSelection = false dialog.canChooseDirectories = isFolder dialog.canChooseFiles = !isFolder if !fileTypes.isEmpty { dialog.allowedFileTypes = fileTypes } if !openFinder.isEmpty, let url = URL(string: openFinder) { dialog.directoryURL = url } if dialog.runModal() == .OK, let path = dialog.url?.path { return path } else { return "" } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }