iOS - 如何实现具有多个参数和 afterDelay 的 PerformSelector?
我是一名 iOS 新手。我有一个选择器方法如下 -
- (void) fooFirstInput:(NSString*) first secondInput:(NSString*) second
{
}
我正在尝试实现类似的东西 -
[self performSelector:@selector(fooFirstInput:secondInput:) withObject:@"first" withObject:@"second" afterDelay:15.0];
但这给了我一个错误 -
Instance method -performSelector:withObject:withObject:afterDelay: not found
关于我缺少什么的任何想法?
I am an iOS newbie. I have a selector method as follows -
- (void) fooFirstInput:(NSString*) first secondInput:(NSString*) second
{
}
I am trying to implement something like this -
[self performSelector:@selector(fooFirstInput:secondInput:) withObject:@"first" withObject:@"second" afterDelay:15.0];
But that gives me an error saying -
Instance method -performSelector:withObject:withObject:afterDelay: not found
Any ideas as to what I am missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
就我个人而言,我认为更接近您的需求的解决方案是使用 NSInitation。
像下面这样的东西就可以完成工作:
indexPath和dataSource是在同一方法中定义的两个实例变量。
Personally, I think that a closer solution to your needs is the use of NSInvocation.
Something like the following will do the work:
indexPath and dataSource are two instance variables defined in the same method.
因为不存在
[NSObject PerformSelector:withObject:withObject:afterDelay:]
方法。您需要将要发送的数据封装到某个单一 Objective C 对象中(例如 NSArray、NSDictionary、某些自定义 Objective C 类型),然后通过
[NSObject PerformSelector:withObject:afterDelay:]< /code> 众所周知且喜爱的方法。
例如:
Because there is no such thing as a
[NSObject performSelector:withObject:withObject:afterDelay:]
method.You need to encapsulate the data you want to send along into some single Objective C object (e.g. a NSArray, a NSDictionary, some custom Objective C type) and then pass it through the
[NSObject performSelector:withObject:afterDelay:]
method that is well known and loved.For example:
您可以将参数打包到一个对象中,并使用辅助方法来调用原始方法,正如 Michael 和其他人现在所建议的那样。
另一个选项是dispatch_after,它将获取一个块并在某个时间将其排队。
或者,正如您已经发现的,如果您不需要延迟,则可以使用
-performSelector:withObject:withObject:
You can package your parameters into one object and use a helper method to call your original method as Michael, and others now, have suggested.
Another option is dispatch_after, which will take a block and enqueue it at a certain time.
Or, as you've already discovered, if you don't require the delay you can just use
- performSelector:withObject:withObject:
最简单的选择是修改您的方法以采用包含两个参数的单个参数,例如
NSArray
或NSDictionary
(或者添加第二个采用单个参数的方法,解包它,并调用第一个方法,然后延迟调用第二方法)。例如,您可以有类似的内容:
然后要调用它,您可以执行以下操作:
The simplest option is to modify your method to take a single parameter containing both arguments, such as an
NSArray
orNSDictionary
(or add a second method that takes a single parameter, unpacks it, and calls the first method, and then call the second method on a delay).For instance, you could have something like:
And then to call it, you can do:
并用以下方式调用它:
and call it with:
您可以在此处找到提供的所有类型的performSelector:方法:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html
有很多变体,但没有一个版本可以接受多个对象以及延迟。您需要将参数包装在 NSArray 或 NSDictionary 中。
You can find all the types of provided performSelector: methods here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html
There are a bunch of variations but there isn't a version that takes multiple objects as well as a delay. You'll need to wrap up your arguments in an NSArray or NSDictionary instead.
我不喜欢 NSInitation 方式,太复杂了。让我们保持简单和干净:
I dislike the NSInvocation way, too complex. Let’s keep it simple and clean:
我只是做了一些调整,需要调用原始方法。我所做的就是制定一个协议并将我的对象投射到它。
另一种方法是在类别中定义方法,但需要抑制警告(#pragma clang Diagnostic 忽略“-Wincomplete-implementation”)。
I just did some swizzling and needed to call the original method. What I did was making a protocol and cast my object to it.
Another way is to define the method in a category, but would need suppression of a warning (#pragma clang diagnostic ignored "-Wincomplete-implementation").
一种简单且可重用的方法是扩展
NSObject
并实现如下内容:
A simple and reusable way is to extend
NSObject
and implementsomething like:
我只需创建一个自定义对象,将所有参数保存为属性,然后使用该单个对象作为参数
I would just create a custom object holding all my parameters as properties, and then use that single object as the parameter
对于单个参数
对于多个参数
For Single argument
For Multiple arguments