1

I have a class that I want to split across multiple files. I tried using categories, but can't figure out how to make it work.

My class is named UserManager and I want to create UserManager+Amazon and UserManager+Facebook.

The problem is that I do need to access private properties and/or methods in UserManager+Amazon that are implemented in UserManager+Facebook, and vice-versa.

How can I extract methods outside the main UserManager.m file, while maintaining the access to private stuff?

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113

1 Answers1

0

EDIT: @Avi has an excellent idea below, although I have not tested it.

I have also just discovered a solution: Properties for Class and Its Subclasses Only

It works with categories and subclasses. I have tested it with my code and it works. It uses a class extension on the BaseClass.h, complete with an implemented example immediately below the accepted answer.


OLD: I've been struggling with this recently as well. My current idea is to create a third category UserManager+Private that implements all of the private methods and handles the properties via associated objects (http://nshipster.com/associated-objects/). It feels very unwieldy, but it might work for you. I would still be interested in a better solution if one exists.

Community
  • 1
  • 1
CJ Dev
  • 192
  • 7
  • Instead of using associated objects, place these properties into the main class implementation. Declare them in a private interface that your categories import. The implementation of a category method (or property) has to exist in the class somewhere at compile time. It doesn't have to exist in the `@implementation` section which is paired with the `@interface` that declares it. – Avi Jan 26 '16 at 14:11
  • @Avi, But the warnings are still produced, right? And I hate warnings. A simple `.h` and `.m` files demo implementation would help to visualize this solution. – Iulian Onofrei Jan 26 '16 at 14:33
  • No warnings are produced. I do this myself to define aspect-specific interfaces for `NSManagedObject` subclasses. – Avi Jan 26 '16 at 14:40
  • @CJDev, but if the `BaseClass.m` doesn't implement the `- (void)baz` method, a warning is generated, even if it is implemented in `ChildClass.m`. And in my case, I have a lot of methods that are not implemented in the base class, and I don't want to write empty methods in it. – Iulian Onofrei Feb 03 '16 at 10:09