简体   繁体   中英

NSDictionary count of objects (0) differs from count of keys (1) Crashing app after new Xcode

I have an app which will take a plain text file, and for each question, create an entry in a PLIST file with different choices for answers, etc. I have used this same code for a few years, but for some reason since the new Xcode came out, it is now crashing. Can someone give me a hand for what is going on? I have made sure that the plain text file is unchanged from previous years, and formatted properly with \n\n between each question.

-[NSDictionary initWithObjects:forKeys:]: count of objects (0) differs from count of keys (1)'

The line of code it crashes on is:

NSDictionary *outerDict = [NSDictionary dictionaryWithObjects:
                                   [NSArray arrayWithObjects: notes, nil]
                                                              forKeys:[NSArray arrayWithObjects:@"Questions", nil]];

Here is the complete code:

-(void)editTextFile {
    
    NSError *error = nil;

    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"JOSHUASTUDY" ofType:@"txt"];
    NSString *fileContents = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error];
    
    if (error)
        NSLog(@"Error reading file: %@", error.localizedDescription);
  
    NSArray *listArray = [fileContents componentsSeparatedByString:@"\n\n"];
  
    for (id listArrayElement in listArray) {
        NSArray *items = [listArrayElement componentsSeparatedByString:@"\n"];
        NSString *theQuestion = [items objectAtIndex:0];
      NSString *answerA = [items objectAtIndex:1];
       NSString *answerB = [items objectAtIndex:2];
       NSString *answerC = [items objectAtIndex:3];
        NSString *answerD = [items objectAtIndex:4];
        
        NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsPath = [paths objectAtIndex:0];
        NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData33.plist"];
        NSDictionary *innerDict;
        NSString *question;
        NSString *negativepoints;
        NSString *positivepoints;
        NSString *durationofquestion;
        NSString *correctAnswer;
        
        NSString *questiontype;
        NSArray *options;
        negativepoints = @"5";
        positivepoints = @"20";
        durationofquestion = @"30";
        correctAnswer = @"0";
        questiontype = @"1";
        options = [NSArray arrayWithObjects:answerA, answerB, answerC, answerD, nil];
        question = theQuestion;
        
        
        
        
        innerDict = [NSDictionary dictionaryWithObjects:
                     [NSArray arrayWithObjects: correctAnswer, durationofquestion, negativepoints, options, positivepoints, theQuestion, questiontype, nil]
                                                forKeys:[NSArray arrayWithObjects:@"Answer", @"duration_in_seconds", @"negative_points", @"options", @"points", @"question", @"question_type", nil]];
        
        NSMutableDictionary *plistdictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
        
        NSMutableArray *notes=[plistdictionary objectForKey:@"Questions"];
        NSLog(@"NOTES %@", fileContents);
        [notes addObject:innerDict];

        NSDictionary *outerDict = [NSDictionary dictionaryWithObjects:
                                   [NSArray arrayWithObjects: notes, nil]
                                                              forKeys:[NSArray arrayWithObjects:@"Questions", nil]];
        
        
        
        
        
        
        id plist = [NSPropertyListSerialization dataFromPropertyList:(id)outerDict
                                                              format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
        [plist writeToFile:plistPath atomically:YES];

    
    
   
    }

}

Here is a sample of the text file:

1. According to Joshua 1:1, to whom did the Lord speak after the death of Moses?
A. Joshua
B. Eleazar
C. Aaron
D. Caleb

2. According to Joshua 1:1, who was Joshua’s father?
A. Moses
B. Nun
C. Jesse
D. Achan

3. According to Joshua 1:1, in what role did Nun serve under the leadership of Moses?
A. Spokesman
B. General
C. Assistant
D. Priest

4. According to Joshua 1:2, where did the Lord tell Joshua and all the people to go?
A. Over the Jordan
B. Beyond the River Euphrates
C. To Gilgal 
D. To Acacia Grove

5. According to Joshua 1:3, what did God say that he had given to Joshua and the children of Israel?
A. The houses of their enemies 
B. Every place that the sole of his foot would tread upon
C. Vineyards and much livestock
D. Bread from Heaven 

As stated in documentation no nil at end of lists:

Unlike dictionaryWithObjectsAndKeys: and other initializers, dictionary literals specify entries in key-value order. You should not terminate the list of objects with nil when using this literal syntax, and in fact nil is an invalid value

May be it has changed over year? I remember doing dictionaries with nil terminated array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM