Skip to content

Fixed NSString/Swift.String Bridging in Locale
 #1474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CoreFoundation/Locale.subproj/CFLocale.c
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ _CFLocaleCalendarDirection _CFLocaleGetCalendarDirection(void) {
#endif
}

#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX
static CFArrayRef _CFLocaleCopyPreferredLanguagesFromPrefs(CFArrayRef languagesArray) {
CFMutableArrayRef newArray = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks);
if (languagesArray && (CFArrayGetTypeID() == CFGetTypeID(languagesArray))) {
Expand All @@ -1100,7 +1100,7 @@ static CFArrayRef _CFLocaleCopyPreferredLanguagesFromPrefs(CFArrayRef languagesA
#endif

CFArrayRef CFLocaleCopyPreferredLanguages(void) {
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX
CFArrayRef languagesArray = (CFArrayRef)CFPreferencesCopyAppValue(CFSTR("AppleLanguages"), kCFPreferencesCurrentApplication);
CFArrayRef result = _CFLocaleCopyPreferredLanguagesFromPrefs(languagesArray);
if (languagesArray) CFRelease(languagesArray);
Expand Down
12 changes: 6 additions & 6 deletions Foundation/NSLocale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,39 +109,39 @@ extension NSLocale {
open class var isoLanguageCodes: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyISOLanguageCodes()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
identifiers.append(obj as! String)
Copy link
Contributor

@parkera parkera Mar 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the result type here CFStringRef, which is not directly bridgeable to Swift.String?

Copy link
Contributor Author

@pvieito pvieito Mar 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, they are Swift.String. If you try to get any of that properties in the actual Swift Foundation implementation they will throw with the following execution:

Could not cast value of type 'Swift.String' (0x7ffff3e61970) to 'Foundation.NSString' (0x7ffff75a9190).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent this kin of confusion, can we just replace the whole thing with:

_SwiftValue.fetch(CFLocaleCopyISOLanguageCodes()) as? [String] ?? []

That's the least confusing primitive for bridging on Linux.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! I'll commit the changes.

}
return identifiers
}

open class var isoCountryCodes: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyISOCountryCodes()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
identifiers.append(obj as! String)
}
return identifiers
}

open class var isoCurrencyCodes: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyISOCurrencyCodes()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
identifiers.append(obj as! String)
}
return identifiers
}

open class var commonISOCurrencyCodes: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyCommonISOCurrencyCodes()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
identifiers.append(obj as! String)
}
return identifiers
}

open class var preferredLanguages: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyPreferredLanguages()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
identifiers.append(obj as! String)
}
return identifiers
}
Expand All @@ -151,7 +151,7 @@ extension NSLocale {
let values = CFLocaleCreateComponentsFromLocaleIdentifier(kCFAllocatorSystemDefault, string._cfObject)._nsObject
values.enumerateKeysAndObjects(options: []) { (k, v, stop) in
let key = (k as! NSString)._swiftObject
let value = (v as! NSString)._swiftObject
let value = v as! String
comps[key] = value
}
return comps
Expand Down
27 changes: 23 additions & 4 deletions TestFoundation/TestNSLocale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TestNSLocale : XCTestCase {
("test_constants", test_constants),
("test_Identifier", test_Identifier),
("test_copy", test_copy),
("test_availableIdentifiers", test_availableIdentifiers),
("test_staticProperties", test_staticProperties),
("test_localeProperties", test_localeProperties),
]
}
Expand Down Expand Up @@ -110,10 +110,29 @@ class TestNSLocale : XCTestCase {
XCTAssertTrue(locale == localeCopy)
}

func test_availableIdentifiers() {
XCTAssertNoThrow(Locale.availableIdentifiers)
func test_staticProperties() {
let euroCurrencyCode = "EUR"
let spainRegionCode = "ES"
let galicianLanguageCode = "gl"
let galicianLocaleIdentifier = Locale.identifier(fromComponents: [NSLocale.Key.languageCode.rawValue: galicianLanguageCode,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the hoop jump through languageCode.rawValue here? The argument type is [String:String]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any suggestions? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the types are all consistent with Darwin :( so, not an issue that concerns this patch specifically.

NSLocale.Key.countryCode.rawValue: spainRegionCode])

XCTAssertTrue(galicianLocaleIdentifier == "\(galicianLanguageCode)_\(spainRegionCode)")

let components = Locale.components(fromIdentifier: galicianLocaleIdentifier)

XCTAssertTrue(components[NSLocale.Key.languageCode.rawValue] == galicianLanguageCode)
XCTAssertTrue(components[NSLocale.Key.countryCode.rawValue] == spainRegionCode)

XCTAssertTrue(Locale.availableIdentifiers.contains(galicianLocaleIdentifier))
XCTAssertTrue(Locale.commonISOCurrencyCodes.contains(euroCurrencyCode))
XCTAssertTrue(Locale.isoCurrencyCodes.contains(euroCurrencyCode))
XCTAssertTrue(Locale.isoRegionCodes.contains(spainRegionCode))
XCTAssertTrue(Locale.isoLanguageCodes.contains(galicianLanguageCode))

XCTAssertTrue(Locale.preferredLanguages.count == UserDefaults.standard.array(forKey: "AppleLanguages")?.count ?? 0)
}

func test_localeProperties(){
#if os(Android)
XCTFail("Locale lookup unavailable on Android")
Expand Down