Groups | Search | Server Info | Login | Register


Groups > comp.lang.objective-c > #27

Re: Instance Method Declaration Optional?

From Tom Harrington <tph@pcisys.no.spam.dammit.net>
Newsgroups comp.lang.objective-c
Subject Re: Instance Method Declaration Optional?
Organization Atomic Bird
References <0cda4b2f-239d-445f-acf7-ab75c65b8c7e@a17g2000yqk.googlegroups.com>
Date 2011-08-12 10:46 -0600
Message-ID <tph-2645FD.10462012082011@localhost> (permalink)

Show all headers | View raw


In article 
<0cda4b2f-239d-445f-acf7-ab75c65b8c7e@a17g2000yqk.googlegroups.com>,
 PhilG <philg@vavatini.com> wrote:

> Hello,
> 
> I implemented the Notifications example in the Conway and Hillegass
> _iPhone Programming_ book, Ch. 9, and the selector passed to
> addObserver:selector:name:object is defined in the implementation of
> the application delegate but is not declared either in its interface
> or its superclasses or protocols. Okay, that seemed odd so I added
> another arbitrarily named method to respond to proximity change
> notifications. I defined the function in the implementation but,
> again, did not declare it in the interface. The program compiled and
> ran flawlessly. What gives? Are instance method declarations optional
> in this case?

In the first case, when adding notification observers, you're passing in 
a selector but not actually calling the method right away. Whether the 
method the selector refers to is declared doesn't really matter at 
compile time. The notification center will locate the method dynamically 
at run time.

In the second case, the compiler needs to know about a method before you 
attempt to call it. That's the whole purpose of the method declaration-- 
to tell the compiler about a method that it has not actually seen yet.

That means that either (a) you need to provide a method declaration, or 
(b) the method implementation needs to be in the same source file as the 
call AND must appear earlier in the file than the call. I can't tell 
from your description if you added code to call the new method. If you 
did, your method call must have been later in the file than the 
implementation. If the order were reversed, you'd need to provide a 
declaration.

-- 
Tom "Tom" Harrington
Independent Mac OS X developer since 2002
http://www.atomicbird.com/

Back to comp.lang.objective-c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Instance Method Declaration Optional? PhilG <philg@vavatini.com> - 2011-08-12 07:48 -0700
  Re: Instance Method Declaration Optional? Tom Harrington <tph@pcisys.no.spam.dammit.net> - 2011-08-12 10:46 -0600
  Re: Instance Method Declaration Optional? news{@bestley.co.uk (Mark Bestley) - 2011-08-12 17:59 +0100
    Re: Instance Method Declaration Optional? PhilG <philg@vavatini.com> - 2011-08-12 10:20 -0700
    Re: Instance Method Declaration Optional? Greg Parker <gparker@apple.com> - 2011-08-12 13:45 -0700

csiph-web