Odd behaviour with NSInvocation, performSelectorOnMainThread and NSClassFromString
Odd behaviour with NSInvocation, performSelectorOnMainThread and NSClassFromString
- Subject: Odd behaviour with NSInvocation, performSelectorOnMainThread and NSClassFromString
- From: Duncan McGregor <email@hidden>
- Date: Fri, 16 Nov 2007 22:26:42 +0000
Please bear with me, I'm only a Java programmer.
I'm looking to capture the result of performSelectorOnMainThread, and
in particular I'm trying to invoke NSClassFromString, in order to
ensure that I build load QTMovie on the main thread.
With a little help from our Cocoa guys, I've come up with the following
@implementation SelectorHelper : NSObject
+ (id) performSelectorOnMainThread:(SEL) aSelector onTarget:
(NSObject*) theTarget withObject:(id) arg {
NSString* selectorName = aSelector != nil ? NSStringFromSelector
(aSelector) : nil;
NSString* className = NSStringFromClass([theTarget class]);
NSLog(@"Calling performSelectorOnMainThread: %@ onTarget: %@
withObject: %@", selectorName, theTarget, arg);
NSMethodSignature* methodSignature = [theTarget
methodSignatureForSelector: aSelector];
if (!methodSignature) {
NSLog(@"No methodSignature found");
return nil;
} else {
NSLog(@"methodSignature : %@", methodSignature);
}
NSInvocation* invocation = [NSInvocation
invocationWithMethodSignature: methodSignature];
[invocation setTarget: theTarget];
[invocation setSelector: aSelector];
if (arg) {
[invocation setArgument:&arg atIndex:2];
}
[invocation performSelectorOnMainThread: @selector(invoke)
withObject: nil waitUntilDone:YES];
void* result = malloc([methodSignature methodReturnLength]); // TODO
[invocation getReturnValue: result];
NSLog(@"performSelectorOnMainThread returning %@", result);
return result;
}
+ (Class) createClass:(NSString*) className {
Class result = NSClassFromString(className);
NSLog(@"createClass returning %@ of type %@", result, [result class]);
return result;
}
@end
Invoking [SelectorHelper performSelectorOnMainThread: @selector
(createClass) onTarget:SelectorHelper withObject:@"QTMovie"]
gives the following log
Calling performSelectorOnMainThread: createClass: onTarget:
SelectorHelper withObject: QTMovie
methodSignature : NSMethodSignature: types=#@:@ nargs=3
sizeOfParams=12 returnValueLength=4;
createClass returning QTMovie of type QTMovie
performSelectorOnMainThread returning <QTMovie: 0x323930 time scale =
0, duration = 0, rate = 0.000000, tracks = { }>
As I read this (and looking in the debugger), createClass is
returning a Class for QTMovie, but the result of the NSInvocation is
an instance of QTMovie.
Does this make sense to anyone? I fear I must be doing something
stupid, but cannot see what.
Thanks in anticipation
Duncan McGregor
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden