尝试做这样的事情:
// get the localized country name (in my case, it's US English)
let englishLocale : NSLocale = NSLocale.init(localeIdentifier : "en_US")
// get the current locale
let currentLocale = NSLocale.currentLocale()
var theEnglishName : String? = englishLocale.displayNameForKey(NSLocaleIdentifier, value: currentLocale.localeIdentifier)
if let theEnglishName = theEnglishName
{
let countryName = theEnglishName.sliceFrom("(", to: ")")
print("the localized country name is \(countryName)")
}
与这个辅助功能that I found here:
import Foundation
extension String {
func sliceFrom(start: String, to: String) -> String? {
return (rangeOfString(start)?.endIndex).flatMap { sInd in
(rangeOfString(to, range: sInd..<endIndex)?.startIndex).map { eInd in
substringWithRange(sInd..<eInd)
}
}
}
}
我想通了这一点通过研究为this related question。
一个真正的Swift 3答案会使用Locale而不是NSLocale。 – Moritz
是的,我同意,但是,对于使用NSLocale的IOS 8,如果您使用的是Locale,则意味着IOS 8不会返回正确的响应。 –