Subclasses, protocols and properties - compiler warning
Subclasses, protocols and properties - compiler warning
- Subject: Subclasses, protocols and properties - compiler warning
- From: Jonny Taylor <email@hidden>
- Date: Fri, 19 Nov 2010 14:38:14 +0000
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 <NSObject>
@property int genericProperty;
-(void)subclassSpecificImplementationOfGenericFunction;
@end
@interface MyBaseClass : NSObject
@property int genericProperty;
@end
@interface MySubclass : MyBaseClass <MyProtocol>
-(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<MyProtocol> 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 (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