在外部库协议中添加和使用新方法时出现警告 [英] Warning while adding and using a new method in external library protocol
问题描述
我使用外部库,我的一个视图控制器注册为该框架中的类的委托。现在,在一个地方,我想对这个委托类执行一些代码。我正在写一个方法,并在我的委托调用它。
现在,所有工作正常,但我收到一个警告,这个新添加的方法不是
@protocol MyExtendedDelegate< LibraryDelegate>
@optional
- (void)actionTaken;
@end
@interface MyController:UITableController< MyExtendedDelegate> {
}
@end
strong>在我的控制器里面我注册自我作为库控制器的委托
LibraryController * libController = [[LibraryController alloc] init];
libController.delegate = self;
最后,这是一个单独的类中的代码,我调用这个方法: / strong>
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([self.libraryController.delegate respondingToSelector:@selector(actionTaken)]){
[self.libraryController.delegate actionTaken];
}
这是我收到的警告: / p>
- action在协议中找不到
- NSObject可能无法响应actionTaken
我想摆脱这个警告。任何想法。
属性 libraryController.delegate
外部库以符合 LibraryDelegate
。在您从扩展协议中调用该方法之前,尝试向下转到 MyExtendedDelegate
。
if([self.libraryController.delegate conformsToProtocol:@protocol(MyExtendedDelegate)])
{
id< MyExtendedDelegate> extendedDelegate =(id< MyExtendedDelegate>)self.libraryController.delegate;
if([extendedDelegate respondingToSelector:@selector(actionTaken)])
{
[extendedDelegate actionTaken];
}
}
I am using a external library and one of my view controller is registering as delegate for a class in that framework. Now, at one place I want to execute some code on this delegate class. I am writing a method for that and calling it on my delegate.
Now, all works fine but I am getting a warning that this newly added method is not part of the protocol.
This is my Class:
@protocol MyExtendedDelegate <LibraryDelegate>
@optional
- (void)actionTaken;
@end
@interface MyController : UITableController <MyExtendedDelegate> {
}
@end
And inside my controller I am registering self as delegate for library controller
LibraryController *libController = [[LibraryController alloc] init];
libController.delegate = self;
Finally, This is the code in a separate class where I am calling this method:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([self.libraryController.delegate respondsToSelector:@selector(actionTaken)]) {
[self.libraryController.delegate actionTaken];
}
Here is the warning I am getting:
-- actionTaken not found in protocol
-- NSObject may not respond to actionTaken
I want to get rid of this warning. Any idea.
The property libraryController.delegate
is defined in the external library to conform to LibraryDelegate
. Try to downcast to MyExtendedDelegate
before you call the method from your extended protocol.
if ([self.libraryController.delegate conformsToProtocol:@protocol(MyExtendedDelegate)])
{
id<MyExtendedDelegate> extendedDelegate = (id<MyExtendedDelegate>)self.libraryController.delegate;
if ([extendedDelegate respondsToSelector:@selector(actionTaken)])
{
[extendedDelegate actionTaken];
}
}
这篇关于在外部库协议中添加和使用新方法时出现警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!