Re: Subclasses, protocols and properties - compiler warning
Re: Subclasses, protocols and properties - compiler warning
- Subject: Re: Subclasses, protocols and properties - compiler warning
- From: Siegfried <email@hidden>
- Date: Fri, 19 Nov 2010 13:25:06 -0200
On 19/11/2010, at 12:38, Jonny Taylor wrote:
> …
>
> Can anybody comment on whether I am doing something strange here
Well I see something strange in here
> Sample code to demonstrate this in a fresh Cocoa project (main.m) with Xcode 3.2.1/gcc 4.2 is as follows:
>
> //==========
> @protocol MyProtocol <NSObject>
> @property int genericProperty;
> -(void)subclassSpecificImplementationOfGenericFunction;
> @end
>
> @interface MyBaseClass : NSObject
> @property int genericProperty;
> @end
This class does not conform to MyProtocol. It defines another property, by the way with the same name.
>
> @interface MySubclass : MyBaseClass <MyProtocol>
> -(void)subclassSpecificImplementationOfGenericFunction;
> @end
Then, this subclass conforms to MyProtocol, so it may implement that property defined in the protocol.
>
> @implementation MyBaseClass
> @synthesize genericProperty;
> @end
This is ok, you synthesized the original class property.
>
> @implementation MySubclass
> -(void)subclassSpecificImplementationOfGenericFunction { return; }
> @end
Now, you haven't implemented the protocol property!
> // I find myself writing "@dynamic genericProperty" here to shut up the compiler warning
That means you're going to provide the implementation directly in runtime. Will you?
> // that reads "warning: property 'genericProperty' requires method '-genericProperty' to be defined - use @synthesize, @dynamic or provide a method implementation"
As explained above, you haven't implemented it and this is referring to the protocol property declaration.
> //==========
>
> Aside from the warning, this compiles and can be tested with the following code which runs correctly, printing out "ok (1, 2)":
>
> //==========
> MySubclass *s = [[MySubclass alloc] init];
> s.genericProperty = 1;
> int a = s.genericProperty;
> id<MyProtocol> s2 = s;
> s2.genericProperty = 2;
> int b = s2.genericProperty;
> printf("ok (%d %d)\n", a, b);
> //==========
It works because you're using the superclass property here._______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden