How is it possible that a category in objective-c is affecting my program if it is not imported anywhere? Its mere existence in a file is changing the outcome of the program.
My UISearchBar broke starting in iOS 7.1. A fellow StackOverflow member suggested that one of my categories may have overridden a function introduced to UISearchBar in iOS 7.1. His suspicion was correct. I tested his theory by slowly commenting out all imports of my UISearchBar categories. After commenting out the last category, my search bar was still broken. In an act of desperation, I physically deleted my UISearchBar category files one by one. I discovered that after deleting the following file, my search bar started working again.
#import "UISearchBar+CustomUI.h"
@implementation UISearchBar (CustomUI)
- (UITextField*)searchField
{
UITextField *searchField = nil;
for (UIView *subview in self.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
searchField = (UITextField *)subview;
break;
}
}
return searchField;
}
@end
Yes, I know the above is fragile code, but there are unconventional design requirements that force programmers to hack.
Even though you have not imported the category, it is probably compiled into the binary. Check the Compile Sources
in Build Phases
and you should see your category there or in the File Inspector
under Target Membership
. Effectively the category will be loaded in runtime and so the methods will be overridden. The import is required to exposed the methods so that you can use them without the compiler warning you.