An NSInvocation is an Objective-C message rendered static, that is, it is an action turned into an object. NSInvocation objects are used to store and forward messages between objects and between applications, primarily by NSTimer objects and the distributed objects system.
[myDummyObject sayHelloWorld];
In fact this line causes the compiler to create a message which says "execute sayHelloWorld method" and sends it to the target object which is myDummyObject here.
SEL selector = @selector(sayHelloWorld);
NSMethodSignature *signature = [myDummyObject methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:selector];
[invocation setTarget:myDummyObject];
[invocation invoke];
-(void)myFunctionWithArg1: (NSString*) arg1 Arg2: (NSString *) arg2 Arg3: (NSString *) arg3 Arg4: (NSString *) arg4
{
NSLog(@"I am a function with 4 arguments: <%@> <%@> <%@> <%@>" , arg1, arg2, arg3, arg4);
}
[myDummyObject myFunctionWithArg1:@"First" Arg2:@"Second" Arg3:@"Third" Arg4:@"Fourth"];
SEL selector = @selector(myFunctionWithArg1:Arg2:Arg3:Arg4:);
NSMethodSignature *signature = [myDummyObject methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:myDummyObject];
[invocation setSelector:selector];
NSString *arg1 = @"First";
NSString *arg2 = @"Second";
NSString *arg3 = @"Third";
NSString *arg4 = @"Fourth";
[invocation setArgument:&arg1 atIndex:2];
[invocation setArgument:&arg2 atIndex:3];
[invocation setArgument:&arg3 atIndex:4];
[invocation setArgument:&arg4 atIndex:5];
[invocation invoke];
-(void)processMyTasksWithCallBackFunction: (NSInvocation *) invocation
{
dispatch_async(dispatch_get_main_queue(), ^{
@autoreleasepool {
NSLog(@"Performing tasks like accessing a web service or preparing data for app");
sleep(3);//waiting 3 seconds
NSLog(@"DONE! Now calling the callback function...");
///////// tasks are done; return the results through the NSInvocation object
///////// NSInvocation is supposed to point to a method which takes 4 NSString arguments
NSString *arg1 = @"Result1";
NSString *arg2 = @"Result2";
NSString *arg3 = @"Result3";
NSString *arg4 = @"Result4";
[invocation setArgument:&arg1 atIndex:2];
[invocation setArgument:&arg2 atIndex:3];
[invocation setArgument:&arg3 atIndex:4];
[invocation setArgument:&arg4 atIndex:5];
[invocation invoke];
}
});
}
-(void)myFunctionWithArg1: (NSString*) arg1 Arg2: (NSString *) arg2 Arg3: (NSString *) arg3 Arg4: (NSString *) arg4
{
NSLog(@"I am a call-back method with 4 arguments: <%@> <%@> <%@> <%@>" , arg1, arg2, arg3, arg4);
}
SEL CB_selector = @selector(myFunctionWithArg1:Arg2:Arg3:Arg4:);
NSMethodSignature *CB_signature = [self methodSignatureForSelector:CB_selector];
NSInvocation *CB_invocation = [NSInvocation invocationWithMethodSignature:CB_signature];
[CB_invocation setTarget:self];//Since we declared the call-back method in current class
[CB_invocation setSelector:CB_selector];
[processMyTasksWithCallBackFunction: CB_invocation];
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Commented:
Voted 'yes'.