Basic Objective-C question

Nibbie
Posts: 3
Joined: 2008.07
Post: #1
This may be an idiotic question, but anyway... Is there a way to convince the compiler that an object responds to a selector?

In a lot of places in my program I take an object of a given class (e.g. Menu), determine its subclass (e.g. StatusMenu) and then call subclass-specific selectors. I've been careful to ensure that each call is preceded by a respondToSelector: check, but of course this doesn't satisfy the compiler and I get a warning. I've not got over 200 of these warnings and it's gotten to the point where I can't see "valid" warnings through all the noise.

Am I going about things entirely the wrong way here? How do you deal with this issue?

Thanks in advance.
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2006.10
Post: #2
You can typecast your object to id or the subclass (e.g., StatusMenu), but if you're having to ask an object what class it is then there's probably something wrong with your design Wink. You said there are a lot of places in your program that take an object of a given class, then ask if it's a given subclass - why not make those places take an object of the subclass, or if you do different things depending on the subclass, why not have different methods for each one?
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #3
Have you included the header for the class you're assuming?
Quote this message in a reply
Nibbie
Posts: 3
Joined: 2008.07
Post: #4
Fenris:

Yes, that's definitely not the issue. The compiler knows what the subclass is, it just doesn't realize the object is of that class.

Norelius:

I agree about the design flaw. I'll definitely do things differently the next time around... live and learn I guess. For now, maybe typecasting is the way to go.

Thanks for the help guys!
Quote this message in a reply
Member
Posts: 254
Joined: 2005.10
Post: #5
I seem to remember that there is some way of silencing an individual warning, but can't remember how. In any case, you should avoid doing that as much as possible because sometimes even though you think the warning is bogus, you might have an assumption wrong and the code could actually be bad.
Quote this message in a reply
⌘-R in Chief
Posts: 1,256
Joined: 2002.05
Post: #6
elliptic Wrote:Fenris:

Yes, that's definitely not the issue. The compiler knows what the subclass is, it just doesn't realize the object is of that class.


The compiler realizes only what you tell it and like Fenris said, it sounds like you're not telling it everything. Paste all of the relevant code.
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2006.10
Post: #7
He said he has a situation like this:

Code:
- (void) doSomething: (NSString*) string
{
    if ([string respondsToSelector: @selector(appendString:)])
    {
        [string appendString: @"Hello world"];
    }
}

There's a compiler warning when calling appendString because NSString doesn't respond to that selector, only its subclass NSMutableString. Hence the suggestion to either typecast or change the method signature.
Quote this message in a reply
⌘-R in Chief
Posts: 1,256
Joined: 2002.05
Post: #8
I misread his first sentence.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Objective C question Wayrath 8 7,492 Apr 15, 2010 07:25 AM
Last Post: Wayrath
  Objective-C property question. proxus 4 4,639 Feb 26, 2009 10:48 PM
Last Post: Josh
  objective-c and c++ question OptimisticMonkey 4 4,637 May 28, 2008 02:51 PM
Last Post: OptimisticMonkey
  Beginner - Objective-C/C++/Engines question LeChuck 8 5,239 Nov 26, 2007 04:26 PM
Last Post: Duane