I want to check if a given string matches a file pattern. Does Swift have builtin support for filename globbing? Or is there any 3rd party packages for this?
I use fnmatch
for this. It’s a C API but quite nice to call from Swift:
let txtMatches = fnmatch("*.txt", "hello.txt", 0) == 0
let jpgMatches = fnmatch("*.txt", "hello.jpg", 0) == 0
print(txtMatches, jpgMatches)
// prints: true false
See the fnmatch
man page for more details.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"