Re: Subclasses, protocols and properties - compiler warning
>> I am encountering what I believe to be a spurious compiler warning. I wonder >> whether this is a clue that I am doing something differently to how I >> "should" do it. The problem comes if I define a protocol containing a >> property and then define that property in a base class that does NOT conform >> to the (whole) protocol. When I subclass that base class, I get warnings >> about how the property is not defined. >> >> // I find myself writing "@dynamic genericProperty" here to shut up the >> compiler warning >> // that reads "warning: property 'genericProperty' requires method >> '-genericProperty' to be defined - use @synthesize, @dynamic or provide a >> method implementation" > > It is my understanding that this is one of the reasons why @dynamic exists. > i.e. I believe that you are already doing the Right Thing. > > From the docs > > "You use the @dynamic keyword to tell the compiler that you will fulfill the > API contract implied by a property either by providing method implementations > directly or at runtime using other mechanisms such as dynamic loading of code > or dynamic method resolution. It suppresses the warnings that the compiler > would otherwise generate if it can’t find suitable implementations. You > should only use it if you know that the methods will be available at runtime." > > Well you know the method will exist at run time since you know your class is > a subclass of a class that implements it. OK fair enough, thanks for replying. It's good to have a second opinion confirm it! I just found it a bit odd because the compiler should very easily be able to tell that the method does exist. Jonny___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Subclasses, protocols and properties - compiler warning
On 19 Nov 2010, at 14:38, Jonny Taylor wrote: > I am encountering what I believe to be a spurious compiler warning. I wonder > whether this is a clue that I am doing something differently to how I > "should" do it. The problem comes if I define a protocol containing a > property and then define that property in a base class that does NOT conform > to the (whole) protocol. When I subclass that base class, I get warnings > about how the property is not defined. > > // I find myself writing "@dynamic genericProperty" here to shut up the > compiler warning > // that reads "warning: property 'genericProperty' requires method > '-genericProperty' to be defined - use @synthesize, @dynamic or provide a > method implementation" It is my understanding that this is one of the reasons why @dynamic exists. i.e. I believe that you are already doing the Right Thing. From the docs "You use the @dynamic keyword to tell the compiler that you will fulfill the API contract implied by a property either by providing method implementations directly or at runtime using other mechanisms such as dynamic loading of code or dynamic method resolution. It suppresses the warnings that the compiler would otherwise generate if it can’t find suitable implementations. You should only use it if you know that the methods will be available at runtime." Well you know the method will exist at run time since you know your class is a subclass of a class that implements it. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Subclasses, protocols and properties - compiler warning
On Nov 19, 2010, at 06:38, Jonny Taylor wrote: > @protocol MyProtocol > @property int genericProperty; > -(void)subclassSpecificImplementationOfGenericFunction; > @end > > @interface MyBaseClass : NSObject > @property int genericProperty; > @end > > @interface MySubclass : MyBaseClass > -(void)subclassSpecificImplementationOfGenericFunction; > @end > > @implementation MyBaseClass > @synthesize genericProperty; > @end > > @implementation MySubclass > -(void)subclassSpecificImplementationOfGenericFunction { return; } > // I find myself writing "@dynamic genericProperty" here to shut up the > compiler warning > // that reads "warning: property 'genericProperty' requires method > '-genericProperty' to be defined - use @synthesize, @dynamic or provide a > method implementation" > @end I believe GCC got stricter with this sort of thing fairly recently. Here's a similar example that I ran into, not involving protocols: @interface MyClass : NSObject @property (readonly) int genericProperty; @end @interface MyMutableClass : MyClass @property (readwrite) int genericProperty; @end @implementation MyClass - (int) genericProperty {...} @end @implementation MyMutableClass // uses the inherited getter - (void) setGenericProperty: (int) newValue {...} @end IIRC, this used to compile without a warning, but now it complains that the getter isn't defined in MyMutableClass. So, I think the problem you ran into isn't about protocols, but rather that adopting the protocol effectively declares the property in the subclass, which now requires a full implementation in the subclass, just like in my example. AFAIK there is no way to determine if the warning is spurious, because there's no sufficiently detailed/complete ObjC language specification against which to ask the question. You could file a bug saying that you don't like the new behavior, I suppose. FWIW I resorted to the same solution you did. I used to be mightily afraid of the mysterious @dynamic, but now it's my friend. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Subclasses, protocols and properties - compiler warning
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 > @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 > -(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 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 (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Subclasses, protocols and properties - compiler warning
I am encountering what I believe to be a spurious compiler warning. I wonder whether this is a clue that I am doing something differently to how I "should" do it. The problem comes if I define a protocol containing a property and then define that property in a base class that does NOT conform to the (whole) protocol. When I subclass that base class, I get warnings about how the property is not defined. 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 @property int genericProperty; -(void)subclassSpecificImplementationOfGenericFunction; @end @interface MyBaseClass : NSObject @property int genericProperty; @end @interface MySubclass : MyBaseClass -(void)subclassSpecificImplementationOfGenericFunction; @end @implementation MyBaseClass @synthesize genericProperty; @end @implementation MySubclass -(void)subclassSpecificImplementationOfGenericFunction { return; } // I find myself writing "@dynamic genericProperty" here to shut up the compiler warning // that reads "warning: property 'genericProperty' requires method '-genericProperty' to be defined - use @synthesize, @dynamic or provide a method implementation" @end //== 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 s2 = s; s2.genericProperty = 2; int b = s2.genericProperty; printf("ok (%d %d)\n", a, b); //== Can anybody comment on whether I am doing something strange here, or whether it's just something the compiler is getting confused about? I think it should have enough information to deduce that "genericProperty" is implemented in the superclass and hence the subclass conforms to the protocol. Thanks for any comments. Jonny___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com