Search code examples
iosobjective-cnsstringnsarraynsdictionary

iOS13 insertObject:atIndex: object cannot be nil - crash / error


I have an app that worked fine until iOS was upgraded to 13. and it now crashes. The app looks up country codes from an array and then displays the relevent description.

In iOS13 we get an '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' crash, when we hit that view

The issue appears to relate to be how NSString stringWithFormat:@"%@-%@" extracts the data.

For example I can replace @"%@-%@" with an option like @"da-DK" - and the app performs as expected, pulling up the "Danish (Denmark) " description, and doesn't crash.

The code is below - anyone have an idea what might have changed in iOS13? Thanks.

+ (NSString *) getTTSDescForDisplay:(NSString *) voiceLanguage {

    NSString *normalizedVoiceLanguage;
    NSArray *array = [voiceLanguage componentsSeparatedByString:@"-"];
    if ([array count] != 2) {
        return nil;
    } else {
        NSString *first = array[0];
        NSString *second = array[1];
        normalizedVoiceLanguage = [NSString stringWithFormat:@"%@-%@",first.lowercaseString,second.uppercaseString];
    }

    NSDictionary *dict = @{
                           @"ar-SA"       :@"Arabic (Saudi Arabia) ",
                           @"cs-CZ"       :@"Czech (Czech Republic) ",
                           @"da-DK"       :@"Danish (Denmark) "                       
                           };

    NSString *str = [dict objectForKey:normalizedVoiceLanguage];
    return str;
}

Solution

  • OK figured it after breakpointing extensively. In the list above I was missing one of the (new? iOS13?) language codes.

    I added it...

    @"en-IN"       :@"English (India) ",
    

    and this fixed the issue.