0

I'm trying to print some basic infos from NSLocale, but I'm not able to get a value back from the .scriptCode property.

Currently, the relevant bits are

let localeIdent = NSLocale.autoupdatingCurrent.identifier
let userLocale = NSLocale(localeIdentifier: localeIdent)
let languageScript = userLocale.scriptCode //not sure why this doesn't seem to return anything.
print("Language script code: \(languageScript)")

the print always returns 'nil'.

The locale returns the rest of the set of information for me, region and language and such, so I'm not sure why this wouldn't be stored / returning.

Matt
  • 217
  • 1
  • 11

1 Answers1

1

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Feel a bit silly -- I thought i'd tried that and still got a nil response, but trying it again now I get the response. Thanks! – Matt Apr 21 '17 at 18:58