0

这有什么问题我得到 EXEC_BAD

self.allLessonsArray = [NSArray arrayWithObjects: [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects:@"Title", @"Description", @"LessonID", @"LessonSuffix", @"LibraryImage", @"Price", @"IsFree", nil] forKeys: [NSArray arrayWithObjects: @"First Lesson", @"This is a test description of our first lesson, this lesson is just a copy of Sample StoryApp which we will replace with our first Color Lesson for Very Small Kids. Design of that lesson is still to be discussed with our designer.", 1, @"Less1", @"LibLess1Image.jpg", 0, 1, nil]], [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects:@"Title", @"Description", @"LessonID", @"LessonSuffix", @"LibraryImage", @"Price", @"IsFree", nil] forKeys: [NSArray arrayWithObjects: @"Second Lesson", @"I want to give some description to this title but its so time consuming and boring to write. So here i go - Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", 2, @"Less2", @"LibLess2Image.png", 0.99, 0, nil]], nil];

格式化:

self.allLessonsArray = [NSArray arrayWithObjects: 
                        [NSDictionary dictionaryWithObjects: 
                         [NSArray arrayWithObjects:
                          @"Title",
                          @"Description",
                          @"LessonID",
                          @"LessonSuffix",
                          @"LibraryImage",
                          @"Price",
                          @"IsFree",
                          nil] 
                                                    forKeys: 
                         [NSArray arrayWithObjects:
                          @"First Lesson", 
                          @"This is a test description of our first lesson, this lesson is just a copy of Sample StoryApp which we will replace with our first Color Lesson for Very Small Kids. Design of that lesson is still to be discussed with our designer.",
                          1, 
                          @"Less1",
                          @"LibLess1Image.jpg",
                          0, 
                          1, 
                          nil]
                         ], 
                        [NSDictionary dictionaryWithObjects: 
                         [NSArray arrayWithObjects:
                          @"Title", 
                          @"Description", 
                          @"LessonID", 
                          @"LessonSuffix",
                          @"LibraryImage", 
                          @"Price",
                          @"IsFree",
                          nil] 
                                                    forKeys: 
                         [NSArray arrayWithObjects: 
                          @"Second Lesson",
                          @"I want to give some description to this title but its so time consuming and boring to write. So here i go - Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", 
                          2, 
                          @"Less2",
                          @"LibLess2Image.png", 
                          0.99,
                          0,
                          nil]
                         ], 
                        nil];
4

2 回答 2

5

NSArray对象只能包含对象。每个整数都需要转换为NSNumber.

特别是第二个嵌入NSArray包含

0, 1

应该是

[NSNumber nunberWithInt:0], [NSNumber nunberWithInt:1]

或制作字符串:

@"0", @"1"

为了便于阅读,请将嵌入NSArray的 sNSDictionary拆分成单独的语句。写得更清楚的代码可能更容易发现错误。

例子:

NSArray *lessonKeys = [NSArray arrayWithObjects:@"Title", @"Description", @"LessonID", @"LessonSuffix", @"LibraryImage", @"Price", @"IsFree", nil];
NSArray *lesson1Values = [NSArray arrayWithObjects: @"First Lesson", @"This is a test description of our first lesson, this lesson is just a copy of Sample StoryApp which we will replace with our first Color Lesson for Very Small Kids. Design of that lesson is still to be discussed with our designer.", [NSNumber numberWithInt:0], @"Less1", @"LibLess1Image.jpg", [NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil];
NSArray *lesson2Values = [NSArray arrayWithObjects: @"Second Lesson", @"I want to give some description to this title but its so time consuming and boring to write. So here i go - Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", [NSNumber numberWithInt:2], @"Less2", @"LibLess2Image.png", [NSNumber numberWithFloat:0.99 ], [NSNumber numberWithInt:0], nil];

NSDictionary *lesson1 = [NSDictionary dictionaryWithObjects: lesson1Values forKeys: lessonKeys];
NSDictionary *lesson2 = [NSDictionary dictionaryWithObjects: lesson2Values forKeys: lessonKeys];

self.allLessonsArray = [NSArray arrayWithObjects: lesson1, lesson2, nil];

我猜测更好的 var 名称。数组也可以进行更好的格式化。注意消除了重复的NSArray课程密钥。

在读取的 plist 文件中,这可能会更好。这将允许在不需要更改代码的情况下进行更改和添加课程。

于 2012-04-08T13:32:12.650 回答
2

这些字面数字是问题所在。集合必须包含 NSObject。如果你用 NSNumbers 替换它们(并确保键和值的计数匹配)它应该没问题。

于 2012-04-08T13:30:30.403 回答