How do I call performSelectorOnMainThread: with an selector that takes > 1 arguments?

前端 未结 3 975
不知归路
不知归路 2021-01-30 15:19

A typical call to performSelectorOnMainThread: looks like this:

[target performSelectorOnMainThread:action withObject:foo waitUntilDone:NO];
         


        
3条回答
  •  日久生厌
    2021-01-30 15:37

    If you wish to preserve the method signature of the receiver then I think you'll need to look at using NSInvocation which allows you to specify multiple argument values.

    You could wrap your call and use a dictionary as a container for your arguments as suggested in another answer but to me this seems like a bit of a code smell.

    A better solution along this line would be to create a class that encapsulates the argument values - i.e. a strongly typed approach. So for example instead of passing firstname, surname, you'd pass an instance of a Person class. This is probably a better route to go down because methods with fewer arguments can yield cleaner code - but that's a whole other story.

提交回复
热议问题