使用标量参数调用选择器 [英] Invoke selector with scalar argument
本文介绍了使用标量参数调用选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前使用 objc_msgSend
在对象集合上调用这样的选择器.有没有更好的方法来做到这一点?这是我的代码:
I currently use objc_msgSend
to invoke such selector on collection of object. Is there any better way to do that? Here is my code:
@protocol ADelegateProtocol {
-(void) timeToEventOneDidChange:(NSInterval) event1;
-(void) timeToEventTwoDidChange:(NSInterval) event1;
}
- (void) delegatesPerformSelector:(SEL) selector withTimeIntervalAsFristParameter:(NSTimeinterval) timeInterval {
for (id<ADelegateProtocol> delegate in delegates) {
if([delegate respondsToSelector:selector]) {
objc_msgSend(delegate, selector, timeInterval);
}
}
}
选择器作为参数传入,timeInterval
是一个非对象值.
The selector is passed in as a parameter, timeInterval
is a non-object value.
注意:我不想使用 KVO.
Note: I don't want to use KVO.
推荐答案
如果你打算使用 objc_msgSend()
你必须创建一个正确类型转换的函数指针来这样做.依靠可变参数映射到非可变参数并非在所有情况下都有效.
If you are going to use objc_msgSend()
you must create a correctly typecast function pointer to do so. Relying on varargs to map to non-varargs doesn't work in all cases.
即你会想要:
void (*myMessage)(id, SEL, NSTimeInterval) = objc_msgSend;
myMessage(delegate, aSelector, aTimeInterval);
(输入 SO - 将语法视为近似值.:)
(typed into SO -- consider the syntax an approximation. :)
这篇关于使用标量参数调用选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文