Question

One of my classes MyClass is very big and I want to split it into several categories MyClass(A), MyClass(B), ... . My problem is that some of the methods of MyClass+A make use of a @property defined in the class extension MyClass().

  • Can I access it from the category, and if yes: how?

  • If no, is there another way to proceed?

Was it helpful?

Solution

Can I access it from the category, and if yes: how?

Yes. Either:

  1. define the continuation category in a different .h file (xxx_Private.h) and include that into your implementation files
  2. Or, redefine the method in a category interface at the top of the implementation file.

Option 1 is the better solution.

OTHER TIPS

You could declare the property in a private header or redefine it as already suggested. Also the following approaches may sometimes be applicable:

  1. Use the ObjectiveC runtime: id objc_msgSend(id self, SEL op, ...). Care should be taken when using primitive types or structures.

  2. Use key-value coding: [someObject setValue:aValue forKey:@"propertyName"];

  3. Use performSelector: [someObject performSelector:@selector(setFoobar) . . . ], note that this will fire a warning under ARC

  4. Use an NSInvocation

Key-value coding is quite common. The others would be more applicable in libraries and frameworks, though you could certainly use them if called for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top