NSLocale Swift 3
NSLocale
was not renamed, it still exists. Locale
is a
new type introduced in Swift 3 as a value type wrapper
(compare SE-0069 Mutability and Foundation Value Types).
Apparently Locale
has no displayName(forKey:value:)
method,
but you can always convert it to its Foundation counterpartNSLocale
:
public var symbol: String {
return (Locale.current as NSLocale).displayName(forKey: .currencySymbol, value: code) ?? ""
}
More examples:
// Dollar symbol in the german locale:
let s1 = (Locale(identifier:"de") as NSLocale).displayName(forKey: .currencySymbol, value: "USD")!
print(s1) // $
// Dollar symbol in the italian locale:
let s2 = (Locale(identifier:"it") as NSLocale).displayName(forKey: .currencySymbol, value: "USD")!
print(s2) // US$
How to get country code using NSLocale in Swift 3
See Wojciech N.'s answer
for a simpler solution!
Similarly as in NSLocale Swift 3, you have to cast the overlay type Locale
back to its
Foundation counterpart NSLocale
in order to retrieve the country code:
if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
print(countryCode)
}
How to get systemLocaleCountryCode in swift 3?
In Swift3
change to
let systemLocaleCountryCode = (NSLocale.system as NSLocale).object(forKey: .countryCode) as? String
NSCurrentLocaleDidChangeNotification in Swift 3?
According to the NSLocale
reference there is
class let currentLocaleDidChangeNotification: NSNotification.Name
// Notification that indicates that the user’s locale changed.
which you can use as
let notification = NSLocale.currentLocaleDidChangeNotification
Port method swizzle of NSLocale.currentLocale from swift 2.3 to swift 3
current
is a (read-only) computed property of NSLocale
:
open class var current: Locale { get }
You build a selector for Objectice-C property getter with
let originalSelector = #selector(getter: NSLocale.current)
See also SE-0064 Referencing the Objective-C selector of property getters and setters.
How to retrieve a value for NSLocale .scriptCode in Swift
Not all locales have a script code. See the Language and Locale IDs section of the Internationalization and Localization Guide.
Locale identifiers can contain various parts such as the language code, script code, and region code. The script and region codes are optional.
Look at the documentation for Locale scriptCode
for an example:
For example, for the locale “zh-Hant-HK”, returns “Hant”.
Simpler locales such as en_US
or de_DE
don't have a script code.
Related Topics
Monitoring App Switching on Os X
Programmatically Disabling Screenshot in App
Swift 2.0 Keychain Type Errors for Secitemcopymatching
Swift Access to Variable Length Array
Why Swift Closure Not Capture Self
Add Skreferencenode/Skscene to Another Skscene in Spritekit
Spritekit and Scenekit - How to Completely Pause a Game
Cross Platform Aes Encryption Between iOS and Kotlin/Java Using Apples Cryptokit
In Swift, How to Write a Generic Function to Resize an Array
How to Set Exit Code Value for a Command Line Utility in Swift
How to Get Iobluetoothdevice's Battery Level, Using Swift and Appkit (Xcode for MACos)
How to Detect and Make Clickable Links in a Uilabel Not Using Uitextview
Swift - Reorder Uitableview Cells
iOS 13: Threading Violation: Expected the Main Thread
Call Parent Constructor When Init a Subclass in Swift