Ios 乡村采摘员斯威夫特4
我想在swift 4中实现一个本地iOS国家/地区选择器。 目前,我有一个包含国家代码的表格:Ios 乡村采摘员斯威夫特4,ios,swift,pickerview,Ios,Swift,Pickerview,我想在swift 4中实现一个本地iOS国家/地区选择器。 目前,我有一个包含国家代码的表格: var countriesCodes = NSLocale.isoCountryCodes as [String] 我实现了委托,但数据源中存在问题。 如何在国家名称中转换每个国家代码?< /p> < p>您可以使用基金会(以下简称:< /P>)中提供的下列函数将每个国家代码密钥转换为其本地化显示名称。 您可以使用flatMap将国家代码数组转换为显示名称: let countryNames = c
var countriesCodes = NSLocale.isoCountryCodes as [String]
我实现了委托,但数据源中存在问题。
如何在国家名称中转换每个国家代码?< /p> < p>您可以使用基金会(以下简称:< /P>)中提供的下列函数将每个国家代码密钥转换为其本地化显示名称。 您可以使用
flatMap
将国家代码数组转换为显示名称:
let countryNames = countriesCodes.flatMap(
Locale.current.localizedString(forRegionCode:))
尽管您可能希望将ISO国家/地区代码及其本地化显示名称组合成一对元组,但这将非常有用:
let countryCodesAndNames = countriesCodes.flatMap { code in
Locale.current.localizedString(forRegionCode: code).map { (code, $0) }
}
// countryCodesAndNames is type [(String, String)]
// which is (ISO code, displayName)
// eg.
print(countryCodesAndNames[0])
// prints ("AC", "Ascension Island")
如何实现委托,数据源中存在什么问题?
let countryCodesAndNames = countriesCodes.flatMap { code in
Locale.current.localizedString(forRegionCode: code).map { (code, $0) }
}
// countryCodesAndNames is type [(String, String)]
// which is (ISO code, displayName)
// eg.
print(countryCodesAndNames[0])
// prints ("AC", "Ascension Island")