iDevGames Forums
Basic Objective-C question - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: Basic Objective-C question (/thread-2498.html)



Basic Objective-C question - elliptic - Jul 30, 2008 08:19 AM

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.


Basic Objective-C question - Norelius - Jul 30, 2008 10:42 AM

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?


Basic Objective-C question - Fenris - Jul 30, 2008 12:00 PM

Have you included the header for the class you're assuming?


Basic Objective-C question - elliptic - Jul 30, 2008 04:13 PM

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!


Basic Objective-C question - Blacktiger - Aug 1, 2008 08:37 AM

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.


Basic Objective-C question - SethWillits - Aug 1, 2008 10:09 AM

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.


Basic Objective-C question - Norelius - Aug 1, 2008 11:37 AM

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.


Basic Objective-C question - SethWillits - Aug 1, 2008 12:03 PM

I misread his first sentence.