So the next two pieces of code are both running in the main thread, and when we look at other people’s code sometimes they call it directly, sometimes they call it with performSelector, so when I see people asking this question today, I’ll just summarize, [delegate imageDownloader:self didFinishWithImage:image]; [delegate performSelector:@selector(imageDownloader:didFinishWithImage:)withObject:self withObject:image]; 1. PerformSelector is the runtime system’s responsibility to find the method, and does no validation at compile time; If you call compilation directly, it will be checked automatically. If imageDownloader: didFinishWithImage:image: If it doesn’t exist, the direct call will be found at compile time (as soon as it’s written with Xcode), but with performSelector it must be found at run time (when the program crashes); Cocoa supports adding a method to a class at runtime, that is, the method doesn’t exist at compile time, but it does exist at run time, and it must be called using performSelector. So sometimes if you’re using performSelector, for the sake of robustness of your program, you’ll use a check method – (BOOL) respondto selector: SEL aSelector; 2. When calling a method directly, be sure to declare the use of the method in the header file and import the header file. When you use performSelector, you don’t have to import the header file that contains the method object, you just call it with performSelector. 1. – (id)performSelector (SEL)aSelector; – (id)performSelector:(SEL)aSelector withObject:(id)object; – (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2; These three methods, all synchronous execution, independent of the thread, the main thread and child thread can be called successfully. Equivalent to calling the method directly. Use it when you need to call a method dynamically. For example: [self performSelector:@selector(test2)]; With [self test2]; The execution is exactly the same. 2. – (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray *)modes; – (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay; The two methods are executed asynchronously, even if the delay parameter is 0. It can only be executed in the main thread and does not call the aSelector method in child threads. This method can be used when clicking a button in the UI triggers an event that consumes system performance. The button is always highlighted during the execution of the event. In this case, the method can be called to handle the event asynchronously, thus avoiding the above problem. Before the execution time of the method is reached, the cancellation method is: + (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(id)anArgument; + (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget; Note: Call the cancel function before calling this method or at the end of the viewController lifecycle of the method to ensure that it does not leak memory. 3. – (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait modes:(NSArray *)array; – (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait; Both methods, which can be executed on the main thread and child threads, call the main thread’s aSelector method. If wait is set to YES, the main thread will not execute the aSelector method until the current thread finishes executing. Set to NO: Execute the aSelector method on the main thread without waiting for the current thread to finish executing. If the current thread is the main thread, the aSelector method will execute immediately. Note: apple does not allow programmers to outside the main thread of the thread to manipulate, the UI at this point we have to call performSelectorOnMainThread function in the main thread to complete the UI update. 4. – (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait modes:(NSArray *)array; – (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait; Invokes a method in the specified thread. Analysis results are the same as 3. 5. – (void) performSelectorInBackground aSelector: (SEL) withObject: (id) arg three, sample open the child thread runs in the background First create a simple function – (void) fooNoInputs { NSLog(@”Does nothing”); } then call it [self performSelector:@selector(fooNoInputs)]; In the second experiment to see how to pass parameters in a message we create a function with input parameters – (void) fooOneIput:(NSString*) first {NSLog(@”Logs %@”, first); } then call it [self performSelector:@selector(fooOneInput: withObject:@”first”]; (void) fooFirstInput:(NSString*) first secondInput:(NSString*) second {NSLog(@”Logs %@ then %@”, first, second); } and then call it [the self performSelector: @ the selector (fooFirstInput: secondInput:) withObject: @ “first” withObject: @ “second”); The fourth experiment is how to create dynamic functions and then call them? We need to create a selector SEL myTestSelector = @selector(myTest:); – (void)abcWithAAA: (NSNumber *)number {int primaryKey = [number intValue]; NSLog(“%i”, primaryKey); } MethodForSelectors * mfs = [[MethodForSelectors alloc]init]; NSArray *Arrays = [NSArray arrayWithObjects:@”AAA”, @”BBB”, nil]; for ( NSString *array in Arrays ){ SEL customSelector = NSSelectorFromString([NSStringstringWithFormat:@”abcWith%@:”, array]); mfs = [[MethodForSelectors alloc] performSelector:customSelector withObject:0]; } note: updated at 20120606 This method continues to run without error when a non-conforming message is passed in. For example, two arguments should be passed, but only one argument is passed. Or three arguments are passed in and the third argument is not initialized. There’s another way to call another Class Function, but without arguments, we’re going to assume that there are no arguments, so you can do [MFS customSelector]; @implementation ClassForSelectors – (void) fooNoInputs {NSLog(@”Does nothing”); } – (void) fooOneIput:(NSString*) first { NSLog(@”Logs %@”, first); } – (void) fooFirstInput:(NSString*) first secondInput:(NSString*) second { NSLog(@”Logs %@ then %@”, first, second); } – (NSArray *)abcWithAAA: (NSNumber *)number { int primaryKey = [number intValue]; NSLog(“%i”, primaryKey); } – (void) performMethodsViaSelectors { [self performSelector:@selector(fooNoInputs)]; [self performSelector:@selector(fooOneInput:) withObject:@”first”]; [self performSelector:@selector(fooFirstInput:secondInput:) withObject:@”first” withObject:@”second”]; } – (void) performDynamicMethodsViaSelectors { MethodForSelectors * mfs = [MethodForSelectors alloc]; NSArray *Arrays = [NSArray arrayWithObjects:@”AAA”, @”BBB”, nil]; for ( NSString *array in Arrays ){ SEL customSelector = NSSelectorFromString([NSStringstringWithFormat:@”abcWith%@:”, array]); mfs = [[MethodForSelectors alloc] performSelector:customSelector withObject:0]; } } @end @implementation MethodForSelectors – (void)abcWithAAA: (NSNumber *)number { NSLog(“%i”, number); } @end