Swift 5 NSOpenPanel to select ".txt" file, but also display ".csv" ".json" ".py" ".json" and other text files

Viewed 42

Swift 5, I use NSOpenPanel to select ".txt" file, but I tried the following three methods, it can also display ".csv" ".json" ".py" ".json" and other text files, how can I only display the ".txt" file?

let dialog = NSOpenPanel();
                     
# 1
dialog.allowedContentTypes = [.text]

# 2
let txtType2 = UTType(filenameExtension: "txt")
dialog.allowedContentTypes = [txtType2!]

# 3
let txtType3 = UTType.types(tag: "txt", 
                           tagClass: UTTagClass.filenameExtension,
                           conformingTo: nil)
dialog.allowedContentTypes = txtType3


# allowedFileTypes works fine, but it was deprecated in macOS 12.0 
# dialog.allowedFileTypes = ["txt"]

0 Answers
Related