xcode NSMutableArray EXC_BAD_ACCESS (code=1)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10683238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSMutableArray EXC_BAD_ACCESS (code=1)
提问by ajnet
I have no idea why, but my NSMutableArray 'items' will not take more than 5 elements.
我不知道为什么,但我的 NSMutableArray 'items' 不会超过 5 个元素。
Can someone please help? I'm following the Big Nerd Ranch iOS Programming book.
有人可以帮忙吗?我正在关注 Big Nerd Ranch iOS 编程书。
This code works fine:
这段代码工作正常:
NSMutableArray *items = [[NSMutableArray alloc] init];
for (int i = 5; i < 10; i++) {
BNRItem *p = [BNRItem randomItem];
[items addObject:p];
}
However if I change the initial value of i to 4 or less the program crashes when exiting the for loop:
但是,如果我将 i 的初始值更改为 4 或更少,则程序在退出 for 循环时会崩溃:
NSMutableArray *items = [[NSMutableArray alloc] init];
for (int i = 4; i < 10; i++) {
BNRItem *p = [BNRItem randomItem];
[items addObject:p];
}
Error screenshot: http://db.tt/3CdueSYh
错误截图:http: //db.tt/3CdueSYh
回答by Novarg
Change your
改变你的
NSArray *randomNounList = [NSArray arrayWithObjects:@"Bear", @"Spork", "Mac", nil];
to:
到:
NSArray *randomNounList = [NSArray arrayWithObjects:@"Bear", @"Spork", @"Mac", nil];
You forgot @
before "Mac"
你@
之前忘记了"Mac"
Hope it helps
希望能帮助到你
回答by mttrb
In the screenshot you posted in your comments you are adding a C string, "Mac"
, to your randomNounList
array. You need to make this an NSString with an @ symbol.
在截图中你在你发表的评论要添加一个C字符串,"Mac"
以你的randomNounList
阵列。您需要将其设为带有 @ 符号的 NSString。
I suspect the crash is occurring when this entry is randomly selected.
我怀疑是随机选择此条目时发生崩溃。
I'm surprised this compiled, I suspect you are ignoring some warnings.
我很惊讶这个编译,我怀疑你忽略了一些警告。