Skip to content

Instantly share code, notes, and snippets.

@flexaddicted
Created December 19, 2016 09:05
Show Gist options
  • Select an option

  • Save flexaddicted/b9c59e60111444da11dfd48bd41e8984 to your computer and use it in GitHub Desktop.

Select an option

Save flexaddicted/b9c59e60111444da11dfd48bd41e8984 to your computer and use it in GitHub Desktop.
Extensions for URL that allows to check if a specific extension matches the allowed ones using reduce
guard sourceUrl.matches([".dat", ".csv"]) else {
print("Extension not allowed!")
}
// do your stuff here
import Foundation
extension URL {
func matches(_ allowedExtensions: [String]) -> Bool {
if lastPathComponent.isEmpty {
return false
}
let result = allowedExtensions.reduce(false, { result, allowedExtension in
let result = result || lastPathComponent.hasSuffix(allowedExtension)
return result
})
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment