In a look over NSLocale.swift
, I noticed that there are many apis that take or return a String
that is used with parameter or property names that (to me) imply they should actually take or provide custom String
wrappers.
These apis as they exist are simply not "swifty", and should be deprecated and replaced.
Additionally, this permits foundation to pre-validate these identifiers, and to potentially specialize their string storage.
I have here split these apis into the types they imply should exist.
Is it acceptable to implement this?
Locale.Identifier
Current apis on NSLocale:
public init(localeIdentifier string: String)
public var localeIdentifier: String
open class var availableLocaleIdentifiers: [String]
open class func components(fromLocaleIdentifier string: String) -> [String : String]
open class func canonicalLocaleIdentifier(from string: String) -> String
open class func windowsLocaleCode(fromLocaleIdentifier localeIdentifier: String) -> UInt32
open class func localeIdentifier(fromComponents dict: [String : String]) -> String
open class func localeIdentifier(fromWindowsLocaleCode lcid: UInt32) -> String?
How I think it should look:
extension (NS)Locale {
public init(_ : Identifier)
public var identifier: Identifier
open class var availableLocaleIdentifiers: [Identifier]
struct Identifier : RawRepresentable {
var rawValue: String
public var components : [String : String]
public var canonicalized : Identifier
public var windowsLocaleCode : UInt32
init(components dict: [String : String])
init?(windowsLocaleCode lcid: UInt32)
}
}
Locale.ISO codes:
Current apis on NSLocale:
// Locale.ISOLanguageCode
open class var isoLanguageCodes: [String]
open class func characterDirection(forLanguage isoLangCode: String) -> NSLocale.LanguageDirection
open class func lineDirection(forLanguage isoLangCode: String) -> NSLocale.LanguageDirection
// Locale.ISOCountryCode
open class var isoCountryCodes: [String]
// Locale.ISOCurrencyCode
open class var isoCurrencyCodes: [String]
open class var commonISOCurrencyCodes: [String]
How I think it should look:
extension (NS)Locale {
open class var isoLanguageCodes: [ISOLanguageCode]
struct ISOLanguageCode : RawRepresentable {
var rawValue: String
public var characterDirection : NSLocale.LanguageDirection
public var lineDirection : NSLocale.LanguageDirection
}
open class var isoCountryCodes: [ISOCountryCode]
struct ISOCountryCode : RawRepresentable {
var rawValue: String
}
open class var isoCurrencyCodes: [ISOCurrencyCode]
open class var commonISOCurrencyCodes: [ISOCurrencyCode]
struct ISOCurrencyCode : RawRepresentable {
var rawValue: String
}
}
Locale.LanguageIdentifier:
Current apis on NSLocale:
open class var preferredLanguages: [String]
open class func canonicalLanguageIdentifier(from string: String) -> String
How I think it should look:
extension (NS)Locale {
open class var preferredLanguages: [LanguageIdentifier]
struct LanguageIdentifier : RawRepresentable {
var rawValue: String
// Because the parameter in `canonicalLanguageIdentifier(from string: String)` is simply "from"
init(canonicalizing string: String)
}
}