Fun with Objective-C

Posted on

Last week I had the chance to look at my Sempais amazing new iOS app he built on his own which he did for fun. And let me tell you, it was gorgeous. It was far from complete, but what he wrote was an app that would show a mobile version of a browser interface. That interface has his bookmarks neatly organized into specific categories on Chrome.

I thought “wow, that’s amazing! I want to build something like this too” so I decided to have a go at Objective-C this weekend.

This … isn’t a tutorial at all but I thought for self reminder I should put this here.

This link ( ) is my reference to building a simple To-Do list by Apple. The problem was last night I had written this code to add my text objects into the list through this method:

-(void)loadInitialData {
    ToDoItem *item1 = [[ToDoItem alloc] init];
    item1.itemName = @"Buy milk";
    [self.toDoItems addObject:item1];
    ToDoItem *item2 = [[ToDoItem alloc] init];
    item2.itemName = @"Buy eggs";
    [self.toDoItems addObject:item2];
    ToDoItem *item3 = [[ToDoItem alloc] init];
    item3.itemName = @"Read a book";
    [self.toDoItems addObject:item3];

However when I build this, I get an error something like “NSMutableArray exception” ..
looking through the docs ( ) it says that anything addObject gets thrown which is Nil would throw an exception. So the problem mean’t that what I was adding to my array was either of NULL value or my declarations are wrong.

After checking through what I had written last, I discovered this morning my problem was here in this method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
    // catch approproate item in the toDoItems array and set the name of that item to display in the cell
    ToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
    cell.textLabel.text = toDoItem.itemName;
    return cell;

My placeholder was declared in the source as @”reuseIdentifier”, which doesn’t exist . In my Main.Storyboard, under To-Do-List scene > Table View > my cell was named @”reuseIdentifier”.

A quick modification from above to the following method would then insure that the compiler knows where and what to do with my passed data:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListPrototypeCell" forIndexPath:indexPath];
    ToDoItem *toDoItem =[self.toDoItems objectAtIndex:indexPath.row];
    cell.textLabel.text = toDoItem.itemName;
    return cell;

Hitting build would now display my app without errors.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s