Skip navigation.
 
mlRe: call to numberOfRowsInTableView during initialization
FROM : Matt Neuburg
DATE : Mon Jun 12 20:12:22 2006

On Mon, 12 Jun 2006 19:28:26 +0200 (CEST), <email_removed> said:
>        Hello all,
>
> I have a project that builds without error or warnings and runs fine,
>but there's still one detail disturbing me : in some parts of my code
>I put some ``watchdog" NSLog's and at runtime some of them appear (and they
>shouldn't).
>
>  Here are the relevant parts in my code :
>
>In MyDocument.h :
>
>  ...
>  @interface MyDocument: NSDocument {
>      IBOutlet NSTableView *table1;
>      IBOutlet NSTableView *table2;
>      IBOutlet NSTableView *table3;
>  ...
>
> In MyDocument.m :
>
>  ...
>  - (int)numberOfRowsInTableView:(NSTableView *)aTableView
>{
>
>    if (aTableView==table1) {
>    return ...
> }
>    if (aTableView==table2) {
>    return ...
> }
> if (aTableView==table3) {
>    return ...
> }
> // should be unreachable, because there are no other table views
> NSLog(@"This is a bug. Fix it ");
> return 2006;
>}
>
>  ...
>
> At runtime, the error message above is displayed in the console (three
>times,
>I guess this is because there are three table views). The debugger
>shows that this occurs during the initialization phase, when the pointers
>table1,table2 and table3 are still nil (that is, uninitialized) and the
>argument aTableView is not nil.
>
> It looks as though I should have initialized the pointers
>table1,table2,table3
>myself somewhere in the code. But once the correct connections are made in
>the NIB file, there is nothing more to be done, right ?


Correct. Always start your numberOfRowsInTableView: implementation with this
line:

if (!aTableView) return 0;

This protects against exactly the case you are seeing, where you are called
too early while the outlet is still nil. m.

--
matt neuburg, phd = <email_removed>, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
<http://www.amazon.com/gp/product/0596102119>

Related mailsAuthorDate
mlcall to numberOfRowsInTableView during initialization delanoy Jun 12, 19:28
mlRe: call to numberOfRowsInTableView during initialization Matt Neuburg Jun 12, 20:12
mlRe: call to numberOfRowsInTableView during initialization Corbin Dunn Jun 12, 20:27