For the method:
[NSThread detachNewThreadSelector:@selector(method:) toTarget:self withObject:(id)SELECTOR];
How do I pass in a @selector? I tried casting it to (id) to make it compile, but it crashes in runtime.
More specifically, I have a method like this:
+(void)method1:(SEL)selector{
[NSThread detachNewThreadSelector:@selector(method2:) toTarget:self withObject:selector];
}
It crashes. How do I pass in the selector without crashing, so that the new thread can call the selector when the thread is ready?
The problem here isn’t passing a selector to a method, per se, but passing a selector where an object is expected. To pass a non-object value as an object, you can use
NSValue. In this case, you’ll need to create a method that accepts an NSValue and retrieves the appropriate selector. Here’s an example implementation: