Warning while adding and using a new method in external library protocol

為{幸葍}努か 提交于 2019-12-25 03:12:45

问题


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.


回答1:


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];
    }
}



回答2:


Write a new protocol that extends the old one, and conform to that, something like:

@protocol MyNewProtocol <OtherProtocol>
   - (void) myCoolMethod;
@end



回答3:


  • (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if ([self.libraryController.delegate respondsToSelector:@selector(actionTaken)]) { [self.libraryController.delegate performSelector:@selector(actionTaken)]; }

Using performSelector instead of directly calling a method will remove warning for sure.



来源:https://stackoverflow.com/questions/5768725/extending-a-protocol-defined-in-external-lib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!