From 74e10a23c21c21ce8d5cb5172e6c9ce2ca0805a6 Mon Sep 17 00:00:00 2001 From: Kizito Nwose Date: Sat, 23 Feb 2019 11:54:19 +0100 Subject: [PATCH] Add `localizedName` property to Country model. --- CountryPickerView/CountryPickerView.swift | 17 +++++++---------- .../CountryPickerViewController.swift | 8 ++++---- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/CountryPickerView/CountryPickerView.swift b/CountryPickerView/CountryPickerView.swift index 10c240f..6710853 100644 --- a/CountryPickerView/CountryPickerView.swift +++ b/CountryPickerView/CountryPickerView.swift @@ -15,19 +15,16 @@ public enum SearchBarPosition { } public struct Country { - public var name: String - public var code: String - public var phoneCode: String - public var flag: UIImage { + public var name: String + public var code: String + public var phoneCode: String + public var localizedName: String? { + return Locale.current.localizedString(forRegionCode: code) + } + public var flag: UIImage { return UIImage(named: "CountryPickerView.bundle/Images/\(code.uppercased())", in: Bundle(for: CountryPickerView.self), compatibleWith: nil)! } - - internal init(name: String, code: String, phoneCode: String) { - self.name = name - self.code = code - self.phoneCode = phoneCode - } } public func ==(lhs: Country, rhs: Country) -> Bool { diff --git a/CountryPickerView/CountryPickerViewController.swift b/CountryPickerView/CountryPickerViewController.swift index 2ab1eca..e182eb6 100644 --- a/CountryPickerView/CountryPickerViewController.swift +++ b/CountryPickerView/CountryPickerViewController.swift @@ -52,14 +52,14 @@ extension CountryPickerViewController { var header = Set() countriesArray.forEach{ - let name = Locale.current.localizedString(forRegionCode: $0.code) ?? $0.name + let name = $0.localizedName ?? $0.name header.insert(String(name[name.startIndex])) } var data = [String: [Country]]() countriesArray.forEach({ - let name = Locale.current.localizedString(forRegionCode: $0.code) ?? $0.name + let name = $0.localizedName ?? $0.name let index = String(name[name.startIndex]) var dictValue = data[index] ?? [Country]() dictValue.append($0) @@ -145,7 +145,7 @@ extension CountryPickerViewController { let country = isSearchMode ? searchResults[indexPath.row] : countries[sectionsTitles[indexPath.section]]![indexPath.row] - let countryName = Locale.current.localizedString(forRegionCode: country.code) ?? country.name + let countryName = country.localizedName ?? country.name let name = dataSource.showPhoneCodeInList ? "\(countryName) (\(country.phoneCode))" : countryName cell.imageView?.image = country.flag @@ -234,7 +234,7 @@ extension CountryPickerViewController: UISearchResultsUpdating { } searchResults.append(contentsOf: indexArray.filter({ - let countryName = Locale.current.localizedString(forRegionCode: $0.code) ?? $0.name + let countryName = $0.localizedName ?? $0.name return countryName.lowercased().hasPrefix(text.lowercased()) })) }