views:

56

answers:

3

I've got a protocol:

@protocol Gadget <NSObject>

@property (readonly) UIView *view;

- (void) attachViewToParent:(UIView *)parentView;

@end

And an "abstract" base class, with an implementation (as a getter, not shown) of -(UIView *)view:

// Base functionality
@interface AbstractGadget : NSObject {
    UIView *view;
}

@property (readonly) UIView *view;

@end

But when I implement the Gadget protocol in a subclass of AbstractGadget, like so:

// Concrete
@interface BlueGadget : AbstractGadget <Gadget> {
}

- (void) attachViewToParent:(UIView *)parentView;

@end


@implementation BlueGadget

- (void) attachViewToParent:(UIView *)parentView {
    //...
}

@end

I get a compiler error telling me "warning: property 'view' requires method '-view' to be defined." I can make this go away using @dynamic, or adding a stub method:

- (UIView *) view {
    return [super view];
}

But I just want to know if I'm doing something that's not supported, something I shouldn't be doing, or if it's just a limitation / bug in the compiler?

A: 

Here is the rule for variable, property and synthesize in objective-C:

If you have a property, you must have a @synthesize or you declare @dynamic and write the getter and setter method yourself.

So, because you have a property called view, you have to declare @synthesize. That should be it. Nothing to do with @protocol, inheritance

vodkhang
Sorry, I should've been more specific. There's a complete implementation of "-(UIView *) view" in AbstractGadget.
Sophistifunk
Can you show me your code in AbstractGadget implementation as well. Something goes wrong now, because it usually works for me
vodkhang
Since the property is implemented in the base class you use @dynamic in the subclass.
JeremyP
+4  A: 

By declaring the property as @dynamic you are telling the compiler that the property getter (and setter if required) are implemented elsewhere (potentially at runtime). This sounds like a perfectly reasonable use case to me.

See The Docs for more information.

Alan Rogers
+1 the same as my answer but much better and with a link to the docs.
JeremyP
I'll have to test this with Clang and Xcode 4 when I can get my hands on them.
Sophistifunk
+2  A: 

I also came across this exact issue. This is one of situations that @dynamic is there for.

JeremyP