首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >目标C数组初始化抛出EXEC_BAD

目标C数组初始化抛出EXEC_BAD
EN

Stack Overflow用户
提问于 2012-04-08 21:07:19
回答 2查看 137关注 0票数 0

这有什么问题吗?我得到了EXEC_BAD

代码语言:javascript
运行
复制
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];

已格式化:

代码语言:javascript
运行
复制
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];
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-08 21:32:13

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

特别是,第二个嵌入的NSArray包含

代码语言:javascript
运行
复制
0, 1

这应该是

代码语言:javascript
运行
复制
[NSNumber nunberWithInt:0], [NSNumber nunberWithInt:1]

或生成字符串:

代码语言:javascript
运行
复制
@"0", @"1"

为提高可读性,请将嵌入式NSArrayNSDictionary分解为单独的语句。更清晰地编写代码可能会使查找错误变得更容易。

示例:

代码语言:javascript
运行
复制
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];

我会猜测出更好的变量名称。同样,可以对数组进行更好的格式化。请注意,删除了重复的NSArray lessonKeys。

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

票数 5
EN

Stack Overflow用户

发布于 2012-04-08 21:30:30

这些字面数字就是问题所在。集合必须包含NSObjects。如果您将它们替换为NSNumbers (并确保键和值的计数匹配),则应该没问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10063035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档