Gold Challenge Solution with questions


#1

Here is my Bronze/Silver/Gold Challenge Solutions. I am very much a newbie to Obj-C, so any suggestions regarding syntax, best practices, etc. are greatly appreciated. Thanks!

// Bronze Challenge
// Add to BNRItemsViewController

  • (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    return @“Tap to confirm Delete”;
    }

// /////////////////
// Silver Challenge
// Multiple parts, this is where I wanted feedback if this was the best way, or even a good way?

// Add to BNRItemStore.m

  • (instancetype) initPrivate
    {
    self = [super init];
    if (self) {
    _privateItems = [[NSMutableArray alloc] init];

      // add NSString initially to array
     // Also a solution to previous chapter
      [_privateItems addObject:@"No More Items!"];
    

    }
    return self;
    }

  • (BNRItem *)createItem
    {
    // If an object is added, remove string, then re-add it
    [self.privateItems removeObjectIdenticalTo:@“No More Items!”];

    BNRItem *item = [BNRItem randomItem];
    [self.privateItems addObject:item];

    // Inefficient, but I wracked my brain and couldn’t figure out a better way that worked
    [self.privateItems addObject:@“No More Items!”];

    return item;
    }

// Add to BNRItemsViewController.m

  • (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // If the indexPath is at the position for the end of the array, return false for canMoveRowAtIndexPath
    NSArray *items = [[BNRItemStore sharedStore]allItems];
    if (indexPath.row == items.count - 1) {
    return FALSE;
    }
    else return TRUE;
    }

// /////////////
// Gold Challenge
// /////////////

// keep the Silver challenge as-is, then add to BNRItemsViewController.h to conform to UITableViewDelegate

@interface BNRItemsViewController : UITableViewController

// Then add to BNRItemsViewController.m

  • (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
    {
    NSArray *items = [[BNRItemStore sharedStore]allItems];
    NSInteger listCount = items.count-1;

    if (proposedDestinationIndexPath.row == listCount) {
    return sourceIndexPath;
    }
    return proposedDestinationIndexPath;
    }

// This now prevents anything from being moved beyond the last item, and the silver challenge prevents the last item from being moved