简体   繁体   中英

how subclass extending the parent class delegate?

subclass extending the parent class delegate,and subclass worked,but have a warning!!!

parent class:

@protocol ObjADelegate;

@interface ObjA : NSObject

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

- (void)doSth1;

@end

@protocol ObjADelegate <NSObject>

- (void)seeA;

@end

subclass:

#import "ObjA.h"

@protocol ObjBDelegate;

@interface ObjB : ObjA

@property (nonatomic, assign) id<ObjBDelegate,ObjADelegate> delegate;

- (void)dosth2;

@end

@protocol ObjBDelegate <ObjADelegate>

- (void)seeB;

@end

it's worked but have a warning for class objB 's delegate : auto property synthesis will not synthesize property 'delegate';it will be implemented by its superclass , use @dynamic to acknowledge intention

how can i remove this warning?????

The hint is in the warning message. You need to use @dynamic in your implementation to tell the compiler that the properties setters and getters are handled elsewhere (in this case, by the superclass).

@implementation ObjB

@dynamic delegate;

- (void)dosth2 
{

}

@end

parent class: .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

parent class:.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

subclass:.h

#import "ClassA.h"
@protocol ClassBDelegate <NSObject, ClassADelegate>
-(void)nihao;
@end
@interface ClassB : ClassA
@property (nonatomic, assign) id<ClassBDelegate> delegate;
- (void)fun2;
@end

subclass:.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

subclass delegate statement must on header

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM