Created
December 19, 2016 09:05
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| guard sourceUrl.matches([".dat", ".csv"]) else { | |
| print("Extension not allowed!") | |
| } | |
| // do your stuff here |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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