Advice needed about a possible bug in NSMethodSignature or NSInvocation
Advice needed about a possible bug in NSMethodSignature or NSInvocation
- Subject: Advice needed about a possible bug in NSMethodSignature or NSInvocation
- From: Thomas Lachand-Robert <email@hidden>
- Date: Thu, 4 Mar 2004 01:39:17 +0100
Hi all,
I have found something that looks like a nasty bug in Foundation or in
the compiler. In my program, I use a number of protocols and use some
shorcuts like
typedef SomeClass<SomeProtocol> SomeShortcut;
Now you would believe that methods declared as
- (SomeClass<SomeProtocol>*) someMethod;
or
- (SomeShortcut*) someMethod;
are identical in practice, so this is just a convenience. Well, that's
true, as all methods behave correctly and identically in either way,
EXCEPT when called via an NSInvocation.
The small test program below do exactly that, it calls two methods with
equal return type, one declared as (NSObject<NSCopying>*) and the other
as (Copy*) after a "typedef NSObject<NSCopying> Copy". No problem with
a standard call, but I get the following output for a call via an
NSInvocation:
Signature for method 'test1' = @, result address = 0x9a004.
Signature for method 'test2' = ^{NSObject=#}, result address = 0x30d510.
meaning that, not only the signature is considered different, but the
returned objects are different! The signature '@' indicates some
object; but '^{NSObject=#}' means a pointer to a struct having a member
named "NSObject" (???) and value a class '#' (????). Very strange.
Needless to say, everything get back to normal if I write
#define Copy NSObject<NSCopying>
instead of the typedef.
Any insight here ?
Thanks,
Thomas Lachand-Robert
********************** email@hidden
<< Et le chemin est long du projet ` la chose. >> Molihre, Tartuffe.
/********* Test program below, copy and paste in an empty Foundation
tool to try ******/
#import <Foundation/Foundation.h>
@implementation NSObject(addition)
-(NSObject<NSCopying>*) test1 {
return self;
}
typedef NSObject<NSCopying> Copy;
-(Copy*) test2 {
return self;
}
@end
void printMethodSignature(id self, NSString* name)
{
SEL selector = NSSelectorFromString(name);
NSMethodSignature* msign = [self methodSignatureForSelector:selector];
NSInvocation* invocation = [NSInvocation
invocationWithMethodSignature:msign];
[invocation setSelector:selector];
[invocation setTarget:self];
[invocation invoke];
const char* ctype = [msign methodReturnType];
printf("Signature for method '%s' = %s", [name cString], ctype);
id result = nil;
[invocation getReturnValue:&result];
printf(", result address = 0x%x.\n", (int)result);
}
int main()
{
NSString* object = @"some string";
NSLog(@"Test1 -> %@, Test2 -> %@", [object test1], [object test2]);//
this is ok
printMethodSignature(object, @"test1");// ok also
printMethodSignature(object, @"test2");// wrong
return 0;
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.