大写所有字符串根据Locale - Swift

Lua*_*awi 5 ios nslocale swift

我试图根据'Locale'大写所有字符串,但它不起作用.

var text = "istanbul, izmir"

println(text.uppercaseStringWithLocale(NSLocale.currentLocale())) 
Run Code Online (Sandbox Code Playgroud)

在土耳其语中,我的大写是不是我.我的结果是伊斯坦布尔,IZMIR但它应该返回İSTANBUL,İZMİR.

请问我的问题在哪里?

Mar*_*n R 15

NSLocale.currentLocale()是在设备的"设置"中选择的区域设置.如果那是"en_US"那么

text.uppercaseStringWithLocale(NSLocale.currentLocale())
Run Code Online (Sandbox Code Playgroud)

将使用英语语言规则,结果是"ISTANBUL,IZMIR".

您可以在设备设置中选择土耳其语言,也可以明确指定土耳其语区域设置:

let text = "istanbul, izmir"
text.uppercaseStringWithLocale(NSLocale(localeIdentifier: "tr"))
// ?STANBUL, ?ZM?R
Run Code Online (Sandbox Code Playgroud)

斯威夫特3:

text.uppercased(with: Locale(identifier: "tr"))
Run Code Online (Sandbox Code Playgroud)


小智 5

如果要使用当前语言环境,可以使用:

text.localizedUppercaseString
Run Code Online (Sandbox Code Playgroud)