项目中我们有时候会遇到这样的问题,父类里面声明了一个delegate,子类想借用这个delegate,就会继承一下,或者扩展一下。
父类:
@protocol ManDelegate <NSObject>
- (void)myNameIs:(NSString*)name;
@end
@interface Man : NSObject
@property (nonatomic, weak) id<ManDelegate> delegate;
@end
子类:
@protocol FatherDelegte <NSObject>
- (void)mySonIs:(NSString*)son;
@end
@interface Father : Man
@property (nonatomic, weak) id<ManDelegate,FatherDelegte> delegate;
@end
或者
@protocol FatherDelegte <ManDelegate>
- (void)mySonIs:(NSString*)son;
@end
@interface Father : Man
@property (nonatomic, weak) id<FatherDelegte> delegate;
@end
使用没问题,但是会遇到warning:
Auto property synthesis will not synthesize property 'delegate'; it will be implemented by its superclass, use @dynamic to acknowledge intention
这是由于父类已经声明过了,子类再声明也不会重新生成新的方法了。如果对警告没有要求,可以凑合用,但是如果希望代码整洁一点的呢,就要改下了。翠花,上代码:
@dynamic delegate;
- (id<FatherDelegte>)delegate
{
id curDelegate = [super delegate];
return curDelegate;
}
- (void)setDelegate:(id<FatherDelegte>)delegate
{
[super setDelegate:delegate];
}
这样就解决了,其实warning里面已经给解决方案的提示了。