PerformSelectorOnMainthRead iOS를 사용하여 호출로 캐스트 객체
-
14-11-2019 - |
문제
[self performSelectorOnMainThread:@selector(uploadDidEnd:) withObject:foo
waitUntilDone:YES]
.
방법 - (void) uploaddidend : (id) 송신자
보낸 사람인가?Foo 호출은 사용자 정의 클래스입니까?이 보낸 사람을 내 객체에서 어떻게 변환 할 수 있습니까?
나는 던지려고하지만, 나는 를 가지고있다.
'-[_NSThreadPerformInfo CountY]: unrecognized selector sent to instance 0x52bbd0'
.
알림에서는 보낸 사람에서 NSNotification까지 캐스트를 사용하고 알림은 객체 속성을 가지고 있지만 NSTHREAD에서는 찾지 못했습니다.
감사합니다.
해결책
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
}
다른 팁
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.