u16suzuの blog

u16suzu のブログです。

Protocolのスパイク

// SNSPost.h
#import "CustomModel.h"
@protocol hogeDelegate <NSObject>
@required
- (void)fuga;
@end

@interface SNSPost : CustomModel
@property (nonatomic, weak) id <hogeDelegate>delegate;
- (void)hoge;
@end
// SNSPost.m
#import "SNSPost.h"

@implementation SNSPost

-(void)hoge{
    [self.delegate fuga];
}
@end
// Tweet.h
#import <Foundation/Foundation.h>
#import "SNSPost.h"

@interface Tweet : SNSPost
@end
// Tweet.m
#import "Tweet.h"

@interface Tweet () <hogeDelegate>
@end

@implementation Tweet
- (void)fuga{
    NSLog(@"%@", @"fuga");
}
@end

呼び出し

SNSPost*sp = [SNSPost new];
Tweet *tw = [Tweet new];
sp.delegate = tw;
[sp hoge]; // "fuga"