Basic Objective-C question
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.
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.
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
. 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?
. 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?
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!
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!
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.
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.
He said he has a situation like this:
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.
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.
I misread his first sentence.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Objective C question | Wayrath | 8 | 6,622 |
Apr 15, 2010 07:25 AM Last Post: Wayrath |
|
| Objective-C property question. | proxus | 4 | 4,104 |
Feb 26, 2009 10:48 PM Last Post: Josh |
|
| objective-c and c++ question | OptimisticMonkey | 4 | 4,162 |
May 28, 2008 02:51 PM Last Post: OptimisticMonkey |
|
| Beginner - Objective-C/C++/Engines question | LeChuck | 8 | 4,597 |
Nov 26, 2007 04:26 PM Last Post: Duane |
|

