Cast Object in una chiamata con performselectoronMainThread Ios
-
14-11-2019 - |
Domanda
Se uso
[self performSelectorOnMainThread:@selector(uploadDidEnd:) withObject:foo
waitUntilDone:YES]
.
nel metodo - (void) uploaddidend: (ID) mittente
Quale oggetto è il mittente?Nel chiamare Foo è una classe personalizzata?Come posso convertire questo mittente nel mio oggetto?
Sto cercando di lanciare ma sto avendo
'-[_NSThreadPerformInfo CountY]: unrecognized selector sent to instance 0x52bbd0'
.
Nelle notifiche, uso un cast dal mittente a nsnotificazione e la notifica ha la proprietà dell'oggetto, ma per NSTHREAD non ho trovato.
Grazie.
Soluzione
You can think of:
[self performSelectorOnMainThread:@selector(uploadDidEnd:)
withObject:foo
waitUntilDone:YES]
as sort of the same as:
[self uploadDidEnd:foo];
Are you sure that's what you're trying to do?
If so: inside your uploadDidEnd:
implementation, you can cast foo to whatever object type it really is like so:
- (void)uploadDidEnd:(id)sender
{
FooClass *foo = (FooClass *)sender;
[foo doSomething]; // or whatever
}
Altri suggerimenti
The argument declared a
sender
is the only parameter passed to your method. performSelectorOnMainThread:withObject:waitUntilDone: passes its argument afterwithObject
to the selector to be performed, so yes, herefoo
will be passed to uploadDidEnd:Casting is not a solution. Objective-C is a dynamic language, so even if casting eliminates compiler warnings, the actual implementation of the object wouldn't change, so it naturally would not repond to messages.