使用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和notification具有对象属性,但对于我未找到的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.