Question on numberOfRowsInTableView

Hi all,

After we add the Protocol to the .h file we need to implement - in the .m file - the 3 methods that are required. One of them is [color=#00BFFF] numberOfRowsInTableView:[/color].

If tasks has not been created yet then [self.tasks count] should return an error (is this right?). Therefore I believe one should check this first.

Hence, my question is: why don’t we make the code as following?

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    //this table-view shows the array of activities, hence the number of elements in the table = the number of elements in the array (= [self.tasks count])
    NSInteger numberOfRows = 0;
    if (self.tasks){
        numberOfRows =  [self.tasks count];
    }
    return numberOfRows;
}

A method invoked on a nil object will return zero.

And tasks being nil implies that there is no table to display.

Therefore, this will do the job nicely:

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView { return [self.tasks count]; }
However, as you have done, first checking to make sure that an object is not nil before sending a message to it is a good thing to do in more complex scenarios to avoid surprises.

great - thx ibex10. I should read the documentation more carefully, sometimes information like that are given in just few words.