NSMutableArray上的无效摘要

时间:2011-05-18 09:23:32

标签: xcode ipad nsmutablearray nskeyedarchiver cgpoint

我需要一些关于如何解决这个问题的专家建议。我正在为iPad的新应用程序做一些原始测试。

我通过加载在另一个应用程序中创建的plist文件,在我的视图控制器的viewDidLoad中创建一个NSMutableArray(ballPath)(它在我的.h文件中声明)。

ballPath = [[NSMutableArray alloc] initWithCapacity:1000];

NSString *path=[[NSBundle mainBundle] pathForResource:@"level_1" ofType:@"plist"];

ballPath = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

此数组现在包含许多CGPoints(由另一个应用程序存储为NSValues和已存档)。

enter image description here

然后我绘制路径(仍然在我的viewDidLoad中),它工作正常,所以路径应该可以正常工作。

当我稍后想要读取数组作为对加速度变化的响应时,我得到一个EXC_BAD_ACCESS。当我调试并查看我的数组时,它看起来像这样:

enter image description here

我用这个(也在viewDidLoad中)测试并替换加载的数组:

    ballPath = [[NSMutableArray alloc] initWithObjects:
            [NSValue valueWithCGPoint:CGPointMake(100.0, 100.0)],
            [NSValue valueWithCGPoint:CGPointMake(100.0, 200.0)],
            [NSValue valueWithCGPoint:CGPointMake(100.0, 300.0)],
            [NSValue valueWithCGPoint:CGPointMake(100.0, 400.0)],
            [NSValue valueWithCGPoint:CGPointMake(125.0, 450.0)],
            [NSValue valueWithCGPoint:CGPointMake(150.0, 500.0)],
            [NSValue valueWithCGPoint:CGPointMake(300.0, 600.0)],
            [NSValue valueWithCGPoint:CGPointMake(350.0, 550.0)],nil];

然后它运作得很好!

enter image description here

我在这里错过了什么????

我在Xcode 4.0.2上,我的目标是iOS 4.3。

1 个答案:

答案 0 :(得分:1)

此时数组被释放,因此以前有一些随机存储器。

unarchiveObjectWithFile:返回一个自动释放的对象,如果你想保持数组,你需要保留它(或使它成为一个保留属性)。之前的alloc-init两行完全是多余的(可能会泄漏内存),因为你从未对你在那里创建的数组做任何事情,它是从你从bundle加载的数组替换