In my iOS application I have a 5 view controllers that all deal with the same feature (groups). These view controllers can be pushed on top of eachother in a few different configurations. I made a file called GroupViewHelper.h which uses @implementation to provide some functions for the groups feature. The functions look through the view controller stack and send a “refresh” message to a view controller of a specific type. The file looks like this:
@implementation UIViewController (GroupViewHelper)
- (void) refreshManageGroupsParent
{
// ...
}
- (void) refreshGroupDetailsParent
{
// ...
}
@end
My code works great and everything behaves as expected, but I get 14 warnings that are all very similar to this at build time:
ld: warning: instance method 'refreshGroupDetailsParent' in category from /Users/x/Library/Developer/Xcode/DerivedData/myapp-ayshzmsyeabbgqbbnbiixjhdmqgs/Build/Intermediates/myapp.build/Debug-iphonesimulator/myapp-dev.build/Objects-normal/i386/GroupMembersController.o conflicts with same method from another category
I think I’m getting this because I’m using a .H which is included in multiple places, but how do I correctly use @implementation in this situation?
Well, sort of, but the real problem is that you’ve put the
@implementationin the.hfile in the first place. If you only included that.hfile in one place, you would get away with it—but it would still not be the right way to do it.Put it in a file called
GroupViewHelper.m, and add that file to your project’s sources, and put the@interfaceinGroupViewHelper.h.Or, ideally, call them
UIViewController+GroupViewHelper.mandUIViewController+GroupViewHelper.h, because that’s the idiomatic way to name category files. (And if you use Xcode’s “New File…” menu item to create a new Objective-C category file, that’s what it will give you.)In other words, interfaces and implementations for categories on existing classes work exactly the same as interfaces and implementations for new classes.