Can inherited @property not satisfy <protocol> @property?

I have a protocol:

@protocol Gadget <NSObject> @property (readonly) UIView *view; - (void) attachViewToParent:(UIView *)parentView; @end 

And the "abstract" base class with implementation (as a receiver, not shown) -(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 , for example:

 // Concrete @interface BlueGadget : AbstractGadget <Gadget> { } - (void) attachViewToParent:(UIView *)parentView; @end @implementation BlueGadget - (void) attachViewToParent:(UIView *)parentView { //... } @end 

I get a compiler message telling me that "warning: the" view "property requires the definition of the" -view "method." I can do this using @dynamic , or by adding a stub method:

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

But I just want to know if I am doing something that is not supported, something that I should not do, or if it is just a limitation / error in the compiler?

+4
source share
3 answers

By declaring the property as @dynamic, you tell the compiler that the getter (and setter if required) property is implemented elsewhere (potentially at runtime). That sounds perfectly reasonable to me.

See Documents for more details.

+5
source

I also ran into this exact problem. This is one of the situations in which @dynamic exists.

+2
source

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

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

So, since you have a property called view, you need to declare @synthesize. It should be like that. Not related to @protocol, inheritance

0
source

Source: https://habr.com/ru/post/1316374/


All Articles