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);
}
}
}
The selector is passed in as a parameter, timeInterval is a non-object value.
Note: I don’t want to use KVO.
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.I.e. You’d want:
(typed into SO — consider the syntax an approximation. 🙂