【发布时间】:2021-11-21 13:13:39
【问题描述】:
我有一个应用程序,它将获取一个纯文本文件,并且对于每个问题,在 PLIST 文件中创建一个条目,其中包含不同的答案选择等。我已经使用相同的代码几年了,但出于某种原因自从新的 Xcode 出来后,它现在崩溃了。有人可以帮我看看发生了什么吗?我已确保纯文本文件与往年相比没有变化,并且在每个问题之间使用 \n\n 正确格式化。
-[NSDictionary initWithObjects:forKeys:]: count of objects (0) differs from count of keys (1)'
它崩溃的代码行是:
NSDictionary *outerDict = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects: notes, nil]
forKeys:[NSArray arrayWithObjects:@"Questions", nil]];
完整代码如下:
-(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];
}
}
这是文本文件的示例:
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
【问题讨论】:
-
字典初始化时不需要数组末尾的nil
标签: ios xcode nsarray nsdictionary plist