Re: call to numberOfRowsInTableView during initialization
Re: call to numberOfRowsInTableView during initialization
- Subject: Re: call to numberOfRowsInTableView during initialization
- From: Matt Neuburg <email@hidden>
- Date: Mon, 12 Jun 2006 11:12:22 -0700
- Thread-topic: call to numberOfRowsInTableView during initialization
On Mon, 12 Jun 2006 19:28:26 +0200 (CEST), email@hidden 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@hidden, <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>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden