Tuesday, September 04, 2012

iOS Objective-C performSelector static method


It's tested OK to use the following static method calls.
Tested on Xcode Version 4.4.1 (4F1003), Apple LLVM Compiler 4.0
    [[StaticMethodClass classperformSelectorInBackground:@selector(runInThreadMethod)withObject:nil];
    [StaticMethodClass performSelectorInBackground:@selector(runInThreadMethod)withObject:nil];
    [StaticMethodClass performSelectorOnMainThread:@selector(runInThreadMethod)withObject:nil waitUntilDone:NO];
    [StaticMethodClass performSelector:@selector(runInThreadMethod) withObject:nil];
Although, there is no auto-code-completion, but it runs fine.
But when use "target", it's NOT ok to use the Class itself, should use "[StaticMethodClass class]" as the target. like.
 [NSTimer scheduledTimerWithTimeInterval:1.0 target:[StaticMethodClass classselector:@selector(runInThreadMethod) userInfo:nil repeats:NO];

Monday, September 03, 2012

iOS Objective-C schedule NSTimer with static method target

Usually we schedule NSTimer like:


    [NSTimer scheduledTimerWithTimeInterval:2.0
             target:instance
             selector:@selector(targetMethod)
             userInfo:nil
             repeats:YES];

targetMethod is an instance method of the instance.

What if we want to schedule to run a static method (or class method) of a class?
Just replace the "instance" with "[ClassA class]".


    [NSTimer scheduledTimerWithTimeInterval:2.0
             target:[ClassA class]
             selector:@selector(staticMethod)
             userInfo:nil
             repeats: YES];

"staticMethod" is a static method of ClassA.
"[ClassA class]" is the target object.