wyu
wyu

Reputation: 1813

NSLocale NSLocaleCalendar causes EXC_BAD_ACCESS

I'm using the follow doc -https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSLocale_Class/index.html#//apple_ref/occ/clm/NSLocale/localeIdentifierFromComponents:

Find localeIdentifierFromComponents(_:) if the link doesn't jump to the correct section of the page.

XCode 7.2 playground

print(NSLocale.localeIdentifierFromComponents([
    NSLocaleLanguageCode:"en",
    NSLocaleCountryCode:"US",
    NSLocaleCalendar: NSJapaneseCalendar
])) 
//EXC_BAD_ACCESS

whereas

print(NSLocale.localeIdentifierFromComponents([
    NSLocaleLanguageCode:"en",
    NSLocaleCountryCode:"US",
]))
//"en_US"

If I use the inverse operation

print(NSLocale.componentsFromLocaleIdentifier("en_US@calendar=japanese"))
//["kCFLocaleCountryCodeKey": "US", "kCFLocaleLanguageCodeKey": "en", "calendar": "japanese"]

NSLocaleCalendar maps to kCFLocaleCalendarKey, not "calendar"

So am I doing something wrong or are the docs just wrong?

Upvotes: 2

Views: 225

Answers (1)

bshirley
bshirley

Reputation: 8356

I would say either the docs are wrong (you are using the exact example that they use) or, more likely, it's an actual bug in the Foundation (Xcode 7.2.1, Swift 2) at least the Swift access to it - this doesn't confirm the Objective-C access does or doesn't work (though I would guess they are the same).

I suggest filing a bug report.

Here's my playground to confirm your error. Even with Gregorian calendar, it's exploding.

//: Playground - noun: a place where people can play

import Foundation

let ident = NSLocale.localeIdentifierFromComponents([
    NSLocaleLanguageCode:"en",
    NSLocaleCountryCode:"US",
    ])

NSGregorianCalendar
NSJapaneseCalendar

NSLocale.componentsFromLocaleIdentifier("en_US")
NSLocale.componentsFromLocaleIdentifier("en_US@calendar=japanese")

let ident2 = NSLocale.localeIdentifierFromComponents([
    NSLocaleLanguageCode:"en",
    NSLocaleCountryCode:"US",
    NSLocaleCalendar:NSGregorianCalendar // causing EXC_BAD_ACCESS
    ])

Upvotes: 1

Related Questions