Re: Getting rid of Obj-C "may not respond to" warning
Re: Getting rid of Obj-C "may not respond to" warning
- Subject: Re: Getting rid of Obj-C "may not respond to" warning
- From: Alastair Houghton <email@hidden>
- Date: Tue, 4 Sep 2007 10:07:01 +0100
On 4 Sep 2007, at 09:43, Chris Suter wrote:
On 04/09/2007, at 6:36 PM, Alastair Houghton wrote:
On 3 Sep 2007, at 21:40, David Dunham wrote:
My application is using an undocumented NSApplication method. I
realize the possible dangers in this, and would like to get rid
of the warning that GCC gives me. Is there a way to do so
(without globally disabling the warning)?
Often you can simply cast the type to "id". GCC can't claim to
know whether an object will respond to a message if the only type
information it has is that it's an object.
I don't believe that will work since the compiler still needs to
know about the return type.
It works fine, provided there is *some* declaration of the method
with that name. It doesn't have to be in a category, because when
you send to an untyped id, GCC will search all the methods it knows
about. If there are multiple methods of that name with different
return types, GCC will pick one (which might not be the one you
expected, so it's worth being careful about that).
If you specify a method name GCC doesn't know (in *any* class or
category), you'll get a (different) warning telling you that it's
going to guess that the return type is "id"...
Here's a test program:
#import <Foundation/Foundation.h>
@interface FooBar : NSObject
{
}
- (int)baz;
- (float)bat;
@end
@interface Foo : NSObject
{
}
- (float)bar;
@end
int main (void)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
float a, b, c;
Foo *foo = [[[Foo alloc] init] autorelease];
a = [foo bar];
b = [(id)foo baz];
c = [(id)foo bat];
NSLog (@"%f, %f, %f\n", a, b, c);
[pool release];
return 0;
}
@implementation Foo
- (float)bar { return 1.3; }
- (int)baz { return 1.3; }
- (float)bat { return 1.3; }
@end
which builds with no warnings (with -W -Wall on the command line),
and displays:
2007-09-04 09:53:29.589 rttst[380] 1.300000, 1.000000, 1.300000
Kind regards,
Alastair.
--
http://alastairs-place.net
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden