如何写delegate的继承

项目中我们有时候会遇到这样的问题,父类里面声明了一个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里面已经给解决方案的提示了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,323评论 19 139
  • 今天的状态还不错,看了教育学,收到了佳佳学姐中国政法大学复试顺利的消息,真心为她高兴,后天梦帆学姐去上师大复试,身...
    征人随说阅读 929评论 0 0
  • 大脑是混沌的
    不明觉厉Y阅读 942评论 0 1
  • (一)昨日总结 96天 昨天一大早起誓,要关机,然后开始了远离手机的生活。我认真学习,静心和自己相处。直到下午六点...
    青衣雨翼_shape阅读 3,051评论 0 0