2011-05-18 57 views
1

我需要一些關於如何解決這個問題的專家建議。我正在爲iPad的新應用程序進行一些粗略測試。NSMutableArray無效彙總

我通過加載在另一個應用程序中創建的plist文件,在我的視圖控制器(它在我的.h文件中聲明)的viewDidLoad中創建一個NSMutableArray(ballPath)。

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

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

ballPath = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; 

此數組現在包含許多CGPoint(由另一個應用程序存儲爲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

該數組在此時被釋放,以便在數組之前存在一些隨機存儲器。

unarchiveObjectWithFile:返回一個自動釋放對象,如果你想保留數組,你需要保留它(或使它成爲一個保留屬性)。之前的alloc-init兩行是完全多餘的(可能會泄漏內存),因爲你從來沒有對你在那裏創建的數組做任何事情,它是從你從bundle中加載的數組中取代取代

+0

謝謝!我現在創建另一個數組(arrayToLoad),加載存檔文件,然後執行ballPath = [arrayToLoad mutableCopy]; – Structurer 2011-05-18 10:59:09