Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to delete objects from NSMutableArray?
    text
    copied!<p>For some reason my code refuses to work. I get a sigbart error in the method<br> <code>- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath</code>, where I try to delete objects from the tableView and my bookmarks Array? (you find the method at the bottom of my code under the pragma mark - Table view data source).</p> <p>I am new to Objective-C and iPhone development, so if you know what may cause this error, please try to explain it with the assumption I am not experienced. Thanx !</p> <p>bookmarks.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "ShowTaskViewController.h" #import "Bookmark.h" @interface BookmarksViewController : UITableViewController &lt;UITableViewDelegate,UITableViewDataSource&gt; { NSMutableArray *bookmarks; } @property (nonatomic, retain) NSMutableArray *bookmarks; @end </code></pre> <p>bookmarks.m</p> <pre><code>#import "BookmarksViewController.h" @interface BookmarksViewController () @end @implementation BookmarksViewController @synthesize bookmarks; - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Uncomment the following line to preserve selection between presentations. // self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; } - (void)viewWillAppear:(BOOL)animated { NSUserDefaults *storage = [NSUserDefaults standardUserDefaults]; NSDictionary *storedBookmarks = [NSKeyedUnarchiver unarchiveObjectWithData:[storage objectForKey:@"bookmarks"]]; if (storedBookmarks == nil) { storedBookmarks = [[NSDictionary alloc] init]; } self.bookmarks = [storedBookmarks allValues]; [self.tableView reloadData]; [super viewWillAppear:animated]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.bookmarks count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"BookmarkCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } Bookmark *item = [self.bookmarks objectAtIndex:indexPath.row]; NSArray *chunks = [item.name componentsSeparatedByString: @","]; NSString *name; NSString *book; NSString *chapter; if ([chunks count] &gt; 0) { name = [chunks objectAtIndex:0]; if ([chunks count] &gt; 1) { book = [chunks objectAtIndex:1]; if ([chunks count] &gt; 2) { chapter = [chunks objectAtIndex:2]; } } } cell.textLabel.text = name; cell.detailTextLabel.text = book; return cell; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"FromBookmarksToShowTaskSegue"]) { ShowTaskViewController *vc = [segue destinationViewController]; Bookmark *item = [self.bookmarks objectAtIndex:[self.tableView indexPathForSelectedRow].row]; vc.taskId = item.id; } } /* // Override to support conditional editing of the table view. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the specified item to be editable. return YES; } */ // Override to support editing the table view. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the row from the data source [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [bookmarks removeObjectAtIndex:indexPath.row]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view } } </code></pre> <p><em><strong></em>*</strong>*EDIT: After I changed positions on </p> <p>[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [bookmarks removeObjectAtIndex:indexPath.row];</p> <p><strong>to</strong></p> <pre><code>[bookmarks removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; </code></pre> <p>I get a new SIGBART error in another file with the following code:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "AppDelegate.h" int main(int argc, char *argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } } </code></pre> <p>Any thoughts ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload