I have a table view full of data defined from the user. the array is gathered from nsuserdefaults and is show in a table view controller. i am trying to implement a delete function. it looks fine but when i press delete the error comes
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'
The code i have for Favourites.h
#import <UIKit/UIKit.h>
@interface FavoritesViewController: UITableViewController <UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *favoriteItems;
@end
Then the piece where i initialize
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
favoriteItems= [[NSUserDefaults standardUserDefaults] objectForKey:@"favoritesArray"];
[self.tableView reloadData];
}
I feel like the above code is where the problem is. NSUserDefaults; I expect is an NSArray. So how do I change it too Mutable?
Just for interests sake the following sets up the delete method
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//remove the deleted object from your data source.
//If your data source is an NSMutableArray, do this
[self.favoriteItems removeObjectAtIndex:indexPath.row];
[[NSUserDefaults standardUserDefaults] setObject:favoriteItems forKey:@"favoritesArray"];
[self.tableView reloadData]; // tell table to refresh now
}
}
Really appreciate the help in advance.