以下代码中的EXC_BAD_ACCESS异常

时间:2014-07-21 13:21:38

标签: ios memory-management nsmutablearray automatic-ref-counting

    NSMutableArray *quesArrayForPar0 = [[NSMutableArray alloc] initWithObjects:

[[NSMutableArray alloc] initWithObjects:@"We have a clearly stated vision for the next 5 years.", "-1", nil], 

[[NSMutableArray alloc] initWithObjects:@"Our organization has clearly established strategy to achieve the vision.", "-1", nil],

 [[NSMutableArray alloc] initWithObjects:@"This strategy is implemented uniformly and effectively throughout our organization.", "-1", nil], 

[[NSMutableArray alloc] initWithObjects:@"We are progressing as per our plan to realize this vision within the envisage time horizon.", "-1", nil]

, nil];

我真的不明白为什么上面的代码会抛出异常......!

1 个答案:

答案 0 :(得分:3)

因为在内部可变数组中,您分配了两个不同的对象。一个是Obj-C字符串@""和其他C字符串""

所以我改变了你的代码及其工作原理。

NSMutableArray *quesArrayForPar0 = [[NSMutableArray alloc] initWithObjects:
                                    [[NSMutableArray alloc] initWithObjects:@"We have a clearly stated vision for the next 5 years.", @"-1", nil],
                                    [[NSMutableArray alloc] initWithObjects:@"Our organization has clearly established strategy to achieve the vision.", @"-1", nil],
                                    [[NSMutableArray alloc] initWithObjects:@"This strategy is implemented uniformly and effectively throughout our organization.", @"-1", nil],
                                    [[NSMutableArray alloc] initWithObjects:@"We are progressing as per our plan to realize this vision within the envisage time horizon.", @"-1", nil]
                                    , nil];