skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. You can try to initialize your own calendar with specific type. Try this on a playground

    let indianCalendar = Calendar(identifier: .indian)
    
    indianCalendar.shortMonthSymbols // ["Chaitra", "Vaisakha", "Jyaistha", "Asadha", "Sravana", "Bhadra", "Asvina", "Kartika", "Agrahayana", "Pausa", "Magha", "Phalguna"]
    
    var gregorianCalendar = Calendar(identifier: .gregorian)
    gregorianCalendar.locale = Locale(identifier: "en")
    
    gregorianCalendar.shortMonthSymbols // ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    

    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.

    Login or Signup to reply.
  3. 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

    let identifiers: [Calendar.Identifier] = [.buddhist,.coptic,.chinese,.islamic,.ethiopicAmeteAlem,.ethiopicAmeteMihret,.gregorian,.hebrew,
                                              .indian,.islamicCivil,.islamicTabular,.islamicUmmAlQura, .japanese, .persian, .republicOfChina, .iso8601]
    for identifier in identifiers {
        var calendar = Calendar(identifier: identifier)
        calendar.locale = .current
    
        print(calendar.identifier)
        print(calendar.shortMonthSymbols)
    }
    
    • First of all this will show that shortMonthSymbols is never empty
    • If we print the count of shortMonthSymbols instead we will see that the number of months vary between 12 and 14 for the calendars so assuming 12 months is clearly wrong
    • If I set the locale to nil shortMonthSymbols 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search