在iOS4上,如果我有非公共协议,如:
@protocol HTTPDelegate <NSObject>
@optional
- (void) methodDidFinish:(NSDictionary *) response;
- (void) methodDidFail:(NSString *) error;
@end
我有一个指向代表的指针,如:
id<HTTPDelegate> delegate;
然后,我想选择调用该委托方法:
if( [delegate respondsToSelector:@selector(methodDidFail:)] ) {
[delegate methodDidFail:errorString];
}
这很有效。但是,我后来决定使用NSError *来解决错误并将协议更改为:
@protocol HTTPDelegate <NSObject>
@optional
- (void) methodDidFinish:(NSDictionary *) response;
- (void) methodDidFail:(NSError *) error;
@end
如果我只是在可选协议方法中更改一个参数的类型,那么当我检查时,编译器不会抱怨(使用respondsToSelector :)如果委托实现了该方法,它将允许我使用methodDidFail传递errorString : 信息。相反,稍后,在运行时,这将导致选择器崩溃无效。
如果我想让编译器抱怨并检查参数的类型怎么办?有没有办法做到这一点?
答案 0 :(得分:1)
没有办法检查参数类型。更好的是在更改类型时添加新方法。我将这样的委托方法命名为:
- (void) methodDidFailWithError:(NSError *) error;
- (void) methodDidFailWithString:(NSString *) errorString;