Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding properties which conform to protocols

I seem to be getting a new error when using LLVM Compiler 2.0, which I haven't had before.

I have a protocol called DTGridViewDelegate defined as:

@protocol DTGridViewDelegate <UIScrollViewDelegate>

I have a property called delegate on DTGridView (a subclass of UIScrollView, which itself has a delegate property). This is defined as:

@property (nonatomic, assign) IBOutlet id<DTGridViewDelegate> delegate;

Now the message I get is:

DTGridView.h:116:63: error: property type 'id<DTGridViewDelegate>' is incompatible with type 'id<UIScrollViewDelegate>' inherited from 'UIScrollView'

Because I had said that the DTGridViewDelegate conforms to UIScrollViewDelegate, I thought that this would be ok to override this property in this way, and indeed this is the first compiler to suggest there is a problem.

I have fixed the error by declaring the property as such:

@property (nonatomic, assign) IBOutlet id<DTGridViewDelegate, UIScrollViewDelegate> delegate;

I am wondering whether this is a compiler issue?

like image 930
Daniel Tull Avatar asked Mar 05 '11 12:03

Daniel Tull


2 Answers

Your setup looks like the same one used in the case of UITableView inheriting from UIScrollView. The UITableViewDelegate protocol inherits from UIScrollViewDelegate protocol.

I set up the following which compiles fine:

// .h
@protocol ParentClassDelegate
-(NSString *) aDelegateMethod;
@end

@interface ParentClass : NSObject {
    id delegate;
}
@property(nonatomic, assign) IBOutlet id <ParentClassDelegate> delegate;
@end

//.m
@implementation ParentClass
@synthesize delegate;

-(id) delegate{
    return @"Parent delegate";
}//-------------------------------------(id) delegate------------------------------------

-(void) setDelegate:(id)someObj{
    delegate=someObj;
}//-------------------------------------(id) setDelegate------------------------------------
@end

//.h
@protocol ChildClassDelegate <ParentClassDelegate>
-(NSArray *) anotherDelegateMethod;
@end

@interface ChildClass : ParentClass{
}
@property(nonatomic, retain) IBOutlet id <ChildClassDelegate> delegate;
@end

//.m
@implementation ChildClass
//@synthesize delegate;

-(id) delegate{
    return @"childDelegate";
}//-------------------------------------(id) delegate------------------------------------

-(void) setDelegate:(id)someObj{
    delegate=someObj;
}//-------------------------------------(id) setDelegate------------------------------------

@end

Not sure what is causing your problem. I would note that in the header the UITableViewDelegate protocol looks like:

@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>

... so maybe the compiler likes things more explicit sometimes.

I would suggest a clean and build. That solves a lot of problems.

like image 145
TechZen Avatar answered Sep 21 '22 19:09

TechZen


Since there isn't a formal Objective-C language specification, it's impossible to say whether the compiler is behaving properly. All we can say is that Apple's gcc doesn't seem to have a problem with the above scenario, though it's conceptually unsound as it can break Liskov substitution, since delegate is covariant from UIScrollView to DTGridView (though covariance is just as much a problem). What would happen if you passed a DTGridView to code expecting a UIScrollView, which then proceeded to set delegate to an object that conformed to UIScrollViewDelegate but not DTGridViewDelegate?

like image 41
outis Avatar answered Sep 20 '22 19:09

outis