PerformSelectoronMainthread iosで呼び出しでオブジェクトをキャストオブジェクト

StackOverflow https://stackoverflow.com/questions/9504298

  •  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
}

他のヒント

  1. The argument declared a sender is the only parameter passed to your method. performSelectorOnMainThread:withObject:waitUntilDone: passes its argument after withObject to the selector to be performed, so yes, here foo will be passed to uploadDidEnd:

  2. 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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top