I have a view controller class that has to implement several protocols. Too keep things neat I have a habit of putting each protocol’s methods in a category on the view controller class.
This time I am getting warnings from the linker that the class does not implement one of the protocols. The methods do work at runtime, the linker just can’t seem to recognize the implementation in the category.
I simplified the class in a different project and I get the same error in the same place.
The class header:
#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
@interface TopVC : UIViewController
<
UINavigationControllerDelegate,
ABPeoplePickerNavigationControllerDelegate
>
{}
@end
The TopVC.m (not shown) is the automatically generated one with no changes. The UINavigationControllerDelegate protocol methods are implemented in this category:
#import <Foundation/Foundation.h>
#import "TopVC.h"
@interface TopVC (UINavigationControllerDelegate)
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
@end
#import "TopVC+UINavigationControllerDelegate.h"
@implementation TopVC (UINavigationControllerDelegate)
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
NSLog(@"navigationController:willShowViewController:animated:");
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
NSLog(@"navigationController:didShowViewController:animated:");
}
@end
The linker does not complain about this methods in this category. However, if I try a category to implement the ABPeoplePickerNavigationControllerDelegate protocol in an identical fashion it complains:
#import "TopVC.h"
@interface TopVC (ABPeoplePickerNavigationControllerDelegate)
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker;
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person;
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier;
@end
#import "TopVC+ABPeoplePickerNavigationControllerDelegate.h"
@implementation TopVC (ABPeoplePickerNavigationControllerDelegate)
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
return YES;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
return YES;
}
@end
The linker complains:
warning: incomplete implementation of class 'TopVC'
warning: method definition for '-peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier:' not found
warning: method definition for '-peoplePickerNavigationController:shouldContinueAfterSelectingPerson:' not found
warning: method definition for '-peoplePickerNavigationControllerDidCancel:' not found
warning: class 'TopVC' does not fully implement the 'ABPeoplePickerNavigationControllerDelegate' protocol
The only difference I can see is that the UINavigationControllerDelegate protocol methods are all optional whereas the ABPeoplePickerNavigationControllerDelegate are all required.
Yet, even though the linker complains, the methods are still called at runtime. I just refuse to have a build with warnings in it. I’ve apparently missed something or made a trivial error somewhere but I can’t spot it.
Hah! I was having a brain blank.
I forgot to move the protocol implementation declaration to the category’s interface like so:
This compiles without warning and works as expected.
I just got lazy when using so many optional protocols which the linker ignores if it can’t find the method implementations.