Ios NSLocale到国家/地区名称
我在objective-C中看到了答案,但我不知道如何转换为swift 我的应用程序从Facebook接收用户的公共信息,我需要将区域设置转换为国家名称Ios NSLocale到国家/地区名称,ios,facebook,swift,locale,nslocale,Ios,Facebook,Swift,Locale,Nslocale,我在objective-C中看到了答案,但我不知道如何转换为swift 我的应用程序从Facebook接收用户的公共信息,我需要将区域设置转换为国家名称 FBRequestConnection.startForMeWithCompletionHandler({ connection, result, error in user["locale"] = result["locale"] user["email"] =
FBRequestConnection.startForMeWithCompletionHandler({
connection, result, error in
user["locale"] = result["locale"]
user["email"] = result["email"]
user.save()
println(result.locale)
})
例如,对于法国用户,代码将“可选(fr_fr)”发送到日志。但是我需要它只发送国家名称。根据localeplanet.com,“fr_fr”的显示名称是“French(France)”。所以在日志中我只想要“法国” 我利用了,迅速地翻译了一篇文章。试试这个:
let locale: NSLocale = NSLocale(localeIdentifier: result.locale!)
let countryCode = locale.objectForKey(NSLocaleCountryCode) as String
var country: String? = locale.displayNameForKey(NSLocaleCountryCode, value: countryCode)
// According to the docs, "Not all locale property keys
// have values with display name values" (thus why the
// "country" variable's an optional). But if this one
// does have a display name value, you can print it like so.
if let foundCounty = country {
print(foundCounty)
}
为Swift 4更新:
FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"locale"]).start { (connection, result, error) in
guard let resultDictionary = result as? [String:Any],
let localeIdentifier = resultDictionary["locale"] as? String else {
return
}
let locale: NSLocale = NSLocale(localeIdentifier: localeIdentifier)
if let countryCode = locale.object(forKey: NSLocale.Key.countryCode) as? String,
let country = locale.displayName(forKey: NSLocale.Key.countryCode, value: countryCode) {
print(country)
}
}
通过对的研究,我迅速地翻译了一篇文章。试试这个:
let locale: NSLocale = NSLocale(localeIdentifier: result.locale!)
let countryCode = locale.objectForKey(NSLocaleCountryCode) as String
var country: String? = locale.displayNameForKey(NSLocaleCountryCode, value: countryCode)
// According to the docs, "Not all locale property keys
// have values with display name values" (thus why the
// "country" variable's an optional). But if this one
// does have a display name value, you can print it like so.
if let foundCounty = country {
print(foundCounty)
}
为Swift 4更新:
FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"locale"]).start { (connection, result, error) in
guard let resultDictionary = result as? [String:Any],
let localeIdentifier = resultDictionary["locale"] as? String else {
return
}
let locale: NSLocale = NSLocale(localeIdentifier: localeIdentifier)
if let countryCode = locale.object(forKey: NSLocale.Key.countryCode) as? String,
let country = locale.displayName(forKey: NSLocale.Key.countryCode, value: countryCode) {
print(country)
}
}