Contents

NSLocale

Information about linguistic, cultural, and technological conventions for use in formatting data for presentation.

Declaration

class NSLocale

Overview

In Swift, this object bridges to Locale; use NSLocale when you need reference semantics or other Foundation-specific behavior.

You typically use a locale to format and interpret information about and according to the user’s customs and preferences.

You can initialize any number of locale instances with init(localeIdentifier:) using one of the locale identifiers found in the availableLocaleIdentifiers array. However, you usually use a locale configured to match the preferences of the current user.

Use the current property to get the locale matching the current user’s preferences. If you need to be alerted when the user does make changes to region settings, register for the currentLocaleDidChangeNotification notification. Alternatively, you can use the autoupdatingCurrent property to get a locale that automatically updates with the user’s configuration settings:

You can inspect a locale by reading its properties, as listed in Getting Information About a Locale. For properties containing a code or identifier, you can then obtain a string suitable for presentation to the user with the methods listed in Getting Display Information About a Locale. For example, you can report the user’s language as a string localized in that language using the autoupdating locale obtained in the previous example:

You frequently use a locale in conjunction with a formatter. For example, the DateFormatter class has a locale property that ensures dates are converted to strings that match the user’s expectations about date formatting. By default, this property indicates the user’s current locale, which is usually the behavior you want, but you can instead set it to another locale instance to obtain a different output. See Data Formatting Guide for more information about working with formatters.

NSLocale is toll-free bridged with its Core Foundation counterpart, CFLocale. See Toll-Free Bridging for more information on toll-free bridging.

Topics

Initializing a Locale

Getting the User’s Locale

Getting Known Identifiers and Codes

Converting Between Identifiers

Getting Information About a Locale

Getting Display Information About a Locale

Accessing Locale Information by Key

Getting the User’s Preferred Languages

Getting Line and Character Direction for a Language

Instance Properties

See Also

Related Documentation

  • Internationalization and Localization Guide
  • Data Formatting Guide