NSMutableArray removeObjectAtIndex 在第二次執行時崩潰 (NSMutableArray removeObjectAtIndex crashes on second execution)


問題描述

NSMutableArray removeObjectAtIndex 在第二次執行時崩潰 (NSMutableArray removeObjectAtIndex crashes on second execution)

I have a tableview with a number of rows that I want to be able to delete. The data that populates the tableview is stored in an NSMutableArray. To delete the data on one row I call:

‑ (void)deleteFromArray:(NSUInteger) number{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];  //load NSUserDefaults
    NSMutableArray *storedArray = [[ NSMutableArray alloc] init];
    storedArray = [prefs objectForKey: @"mydata"];
    [storedArray removeObjectAtIndex:number];

    [prefs setObject:storedArray forKey:@"mydata"];
    [storedArray release];
    [prefs synchronize];

}

From

‑ (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        subscribe *subscribeclass = [subscribe alloc];
        [subscribeclass deleteFromArray: indexPath.row];
        [myTableView reloadData];
    }
}

This works fine the first time I delete a row. But when I delete a seconds row the application crashes on the following line in deleteFromArray:

[storedArray removeObjectAtIndex:number];

Any ideas why? 

‑‑‑‑‑

參考解法

方法 1:

The array coming from your NSUserDefaults is a NSArray, not an NSMutableArray. You should modify your code to be like this:

‑ (void)deleteFromArray:(NSUInteger) number{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];  //load NSUserDefaults
    NSMutableArray *storedArray = [[prefs objectForKey:@"mydata"] mutableCopy];
    [storedArray removeObjectAtIndex:number];

    [prefs setObject:storedArray forKey:@"mydata"];
    [storedArray release];
    [prefs synchronize];
}

This creates a copy of the immutable array and makes it mutable, so that you can modify it.

方法 2:

The things you can only store immutable NSArrays in defaults. Even if the object is mutable, it is stored as immutable

So instead of 

 NSMutableArray *storedArray = [[ NSMutableArray alloc] init];
 storedArray = [prefs objectForKey: @"mydata"];

try:

 [NSMutableArray arrayWithArray:[prefs objectForKey: @"mydata"]];

方法 3:

The problem is that the array returned from objectForKey is immutable. So you can see what other people do:Storing custom objects in an NSMutableArray in NSUserDefaults

(by PonySlayshukirRamy Kfourysunkehappy)

參考文件

  1. NSMutableArray removeObjectAtIndex crashes on second execution (CC BY‑SA 3.0/4.0)

#cocoa-touch #iOS #nsmutablearray #uitableview






相關問題

防止 UITableView 在自定義子視圖處理觸摸時滾動 (Prevent UITableView from scrolling when custom subview is handling touches)

NSMutableArray removeObjectAtIndex 在第二次執行時崩潰 (NSMutableArray removeObjectAtIndex crashes on second execution)

Apakah NSURLConnections termasuk dalam model atau pengontrol saya? (Do NSURLConnections belong in my model or controller?)

Lợi ích của việc sử dụng NSString tĩnh cho CellIdentifier là gì? (Whats the advantage of using a static NSString for CellIdentifier?)

CGContextRef 中的坐標係是什麼樣的? (How does the coordinate system in an CGContextRef look like?)

無法在不崩潰的情況下設置 ABPeoplePickerNavigationController 的 addressBook 屬性 (Can't set the addressBook property of ABPeoplePickerNavigationController without crashing)

tableViewHeader 拒絕多個子視圖 (tableViewHeader rejecting multiple sub-views)

界面生成器添加了一個標籤欄按鈕,但未顯示 (Interface builder added one tab bar button, but not showing up)

iVar getter / 方法同名? (iVar getter / method with same name?)

如何更改主視圖是基於導航的應用程序! (how to change main view is navigation based app!)

捕捉返回按鈕導航事件 (Catching back button navigation event)

如何將對象轉發到單獨的子視圖? (How can i forward objects to separate subviews?)







留言討論