子类扩展父类委托,子类工作,但有警告!!!
父类:
@protocol ObjADelegate;
@interface ObjA : NSObject
@property (nonatomic, assign) id<ObjADelegate> delegate;
- (void)doSth1;
@end
@protocol ObjADelegate <NSObject>
- (void)seeA;
@end
子类:
#import "ObjA.h"
@protocol ObjBDelegate;
@interface ObjB : ObjA
@property (nonatomic, assign) id<ObjBDelegate,ObjADelegate> delegate;
- (void)dosth2;
@end
@protocol ObjBDelegate <ObjADelegate>
- (void)seeB;
@end
它有效,但对班级objB的代表发出警告:auto property synthesis will not synthesize property 'delegate';it will be implemented by its superclass , use @dynamic to acknowledge intention
如何删除此警告?????
答案 0 :(得分:1)
提示位于警告消息中。您需要在实现中使用@dynamic
来告诉编译器属性setter和getter是在别处处理的(在本例中是超类)。
@implementation ObjB
@dynamic delegate;
- (void)dosth2
{
}
@end
答案 1 :(得分:0)
父类:.h
#import <Foundation/Foundation.h>
@protocol ClassADelegate;
@interface ClassA : NSObject{
id _delegate;
}
@property (nonatomic, assign) id<ClassADelegate> delegate;
- (void)fun1;
@end
@protocol ClassADelegate <NSObject>
- (void)hello;
@end
父类:.m
#import "ClassA.h"
@implementation ClassA
@dynamic delegate;
- (void)fun1{
if (self.delegate && [self.delegate respondsToSelector:@selector(hello)]) {
[self.delegate hello];
}
}
- (void)setDelegate:(id<ClassADelegate>)delegate{
_delegate = delegate;
}
- (id<ClassADelegate>)delegate{
return _delegate;
}
@end
子类:.H
#import "ClassA.h"
@protocol ClassBDelegate <NSObject, ClassADelegate>
-(void)nihao;
@end
@interface ClassB : ClassA
@property (nonatomic, assign) id<ClassBDelegate> delegate;
- (void)fun2;
@end
子类:.M
#import "ClassB.h"
@implementation ClassB
@dynamic delegate;
- (void)fun2{
if (_delegate && [_delegate respondsToSelector:@selector(nihao)]) {
[_delegate nihao];
}
}
- (void)setDelegate:(id<ClassBDelegate>)delegate{
_delegate = delegate;
}
- (id<ClassBDelegate>)delegate{
return _delegate;
}
@end
子类委托语句必须在标题
上