开发者

Overriding properties which conform to protocols

开发者 https://www.devze.com 2023-02-15 12:34 出处:网络
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:

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开发者_如何学C 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?


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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消