views:

507

answers:

2

I want to use a category to make a method on the original class available as a property as well.

Class A:

@interface ClassA
- (NSString*)foo;
@end

Class A category

@interface ClassA (Properties)
- (void)someCategoryMethod;
@property (nonatomic, readonly) NSString *foo;
@end

Now when I do this, it seems to work (EDIT: Maybe it doesn't work, it doesn't complain but I am seeing strangeness), but it gives me warnings because I am not synthesizing the property in my category implementation. How do I tell the compiler everything is actually just fine since the original class synthesizes the property for me?

A: 

If something's declared in your category's interface, its definition belongs in your category's implementation.

tedge
But I don't have access to the source of `ClassA`. Let's say it's class in UIKit that I am "improving".
Squeegy
You should probably be creating a subclass, not a category.
Rob Keniger
If it's a subclass, instances of this class created in areas I don't have control over will not have this code and properties.
Squeegy
You might look into method swizzling:http://samsoff.es/posts/customize-uikit-with-method-swizzlinghttp://stackoverflow.com/questions/1637604/method-swizzle-on-iphone-device
tedge
+1  A: 

Here's the warning you're getting:

warning: property ‘foo’ requires method '-foo' to be defined - use @synthesize, @dynamic or provide a method implementation

To suppress this warning, have this in your implementation:

@dynamic foo;

codewarrior
Just what I needed, thank you!
Squeegy