我新的目標C。Accesing可變數組值
我已經創建了的appdelegate所述一個可變數組。我試圖在另一個應用程序中檢索可變數組的值。但在這一點上它正在崩潰。 下面是我在AppDelegate中聲明的代碼:
savedLocation = [[NSMutableArray alloc] init];
savedLocation = [[NSMutableArray arrayWithObjects:
[NSNumber numberWithInteger:0],
nil] retain];
下面的代碼中,我試圖通過的appdelegate訪問其他應用程序的陣列值:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSInteger selection = [[appDelegate.savedLocation objectAtIndex:0] integerValue];
但它在崩潰:
NSInteger selection = [[appDelegate.savedLocation objectAtIndex:0] integerValue];
請告訴我爲何導致崩潰的原因,並做THI的正確方法也是。
什麼是控制檯中的崩潰消息? – nduplessis 2009-11-23 07:38:52
另外,你正在泄漏savedLocation = [[NSMutableArray alloc] init]; 這是多餘的,因爲[NSMutableArray arrayWithObjects:]返回一個新的自動釋放對象。 – nduplessis 2009-11-23 07:40:26
請仔細閱讀Apple的內存管理文檔,因爲您顯然不知道Cocoa如何做到這一點。 – 2009-11-23 13:42:37