Created
September 24, 2021 18:36
-
-
Save dmytro-anokhin/640f2739ef369da731d6d811bdcac16e to your computer and use it in GitHub Desktop.
This file contains 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
protocol CitiesSource { | |
func loadCities() -> [String] | |
} | |
struct CitiesFile: CitiesSource { | |
let location: URL | |
init(location: URL) { | |
self.location = location | |
} | |
/// Looks up for `cities` file in the main bundle | |
init?() { | |
guard let location = Bundle.main.url(forResource: "cities", withExtension: nil) else { | |
assertionFailure("cities file is not in the main bundle") | |
return nil | |
} | |
self.init(location: location) | |
} | |
func loadCities() -> [String] { | |
do { | |
let data = try Data(contentsOf: location) | |
let string = String(data: data, encoding: .utf8) | |
return string?.components(separatedBy: .newlines) ?? [] | |
} | |
catch { | |
return [] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment