Why does the below code returns an empty list of array?
let symbols = Calendar.current.shortMonthSymbols //[]
I assumed that there is always 12 elements in the array, so I simply access it:
let selected = symbols[index] //index depends on what month I need.
I have a crash for some devices here, not for every device. Why is this happening?
3
Answers
By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the locale property first – most likely to Locale.autoupdatingCurrent.
You can try to initialize your own calendar with specific type. Try this on a playground
The month symbols depend on both calendar identifier and locale. This way you can get the desired result. I couldn’t get an empty list, though. Perhaps the current locale on your device is set to some non-standard value and it causes the list to be empty. That might even be a bug in the SDK.
This doesn’t answer the question but it might be helpful in investigating the issue further.
Consider the following test code that creates an array of all possible calendars and then loops over them to print the content of
shortMonthSymbols
shortMonthSymbols
is never emptyshortMonthSymbols
instead we will see that the number of months vary between 12 and 14 for the calendars so assuming 12 months is clearly wrongshortMonthSymbols
will still contain values for all calendars even though for some they contain a generic sequence M01, M02,…So either there is an issue for some very specific locale (perhaps in combination with some calendar) that will generate an empty list or there is some other issue with your code (
index
being incorrect for instance) that causes your crash.