Last active
September 21, 2016 00:44
-
-
Save mactive/0ddba230d8a79d470303111027628b1a to your computer and use it in GitHub Desktop.
Protocol and Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// APerfactDelegate.h | |
// ReactiveCocoaPractice | |
// | |
// Created by mengqian on 20/9/2016. | |
// Copyright © 2016 thinktube. All rights reserved. | |
// | |
#ifndef APerfactDelegate_h | |
#define APerfactDelegate_h | |
@protocol APerfectDelegate <NSObject> | |
@property(nonatomic, readonly)NSString *name; | |
@property(nonatomic, readonly)NSNumber *count; | |
@optional | |
- (void)optionalSel; | |
@required | |
- (void)requriedSel; | |
@end | |
#endif /* APerfactDelegate_h */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ExamViewController.h" | |
#import "Perfact.h" | |
#import "MyClass.h" | |
@interface ExamViewController ()<APerfectDelegate> | |
@end | |
@implementation ExamViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view. | |
self.view.backgroundColor = [UIColor whiteColor]; | |
Perface *per = [[Perface alloc] init]; | |
per.delegate = self; | |
[per actionWithMethodName:nil]; | |
MyClass *cls = [[MyClass alloc]init]; | |
NSLog(@"MyClass's name: %@",cls.name); | |
[cls methodA]; | |
} | |
-(void)optionalSel | |
{ | |
NSLog(@"ExamViewController optionalSel"); | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ProtocolInherits.h | |
// ReactiveCocoaPractice | |
// | |
// Created by mengqian on 21/9/2016. | |
// Copyright © 2016 thinktube. All rights reserved. | |
// | |
@protocol A | |
@property (nonatomic, readonly) NSString* name; | |
@required | |
-(void) methodA; | |
@end | |
// B 协议继承自 A,协议也可以多重继承, 比如 <A, NSObject> | |
@protocol B <A> | |
@required | |
-(void) methodB; | |
@end | |
// =================================== | |
@interface MyClass : NSObject <B> | |
// 协议可以被实例实现, 也可以赋值给变量使用 | |
@property(nonatomic, weak) id <B> delegate; | |
@end | |
@implementation MyClass | |
@synthesize delegate; | |
@synthesize name; // A的name属性也是可以直接用 | |
-(instancetype)init | |
{ | |
self = [super init]; | |
if(self){ | |
[delegate methodB]; // 可以调用B的方法 | |
[delegate methodA]; // 可以调用B继承的A的方法 | |
// 这里这两个方法不会执行, 因为delegate的宿主并不是这个类. 除非self.delegate = self; | |
// 那么会走下面的两个方法 methodB methodA. delegate指向谁,谁负责实现 | |
} | |
return self; | |
} | |
-(void)methodB // 必须要实现 | |
{ | |
} | |
-(NSString *)name{ | |
return @"MyClass"; | |
} | |
-(void)methodA // 必须要实现 | |
{ | |
NSLog(@"methodA: %@",self.name); | |
} | |
@end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Perface.h | |
// ReactiveCocoaPractice | |
// | |
// Created by mengqian on 20/9/2016. | |
// Copyright © 2016 thinktube. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import "APerfactDelegate.h" | |
@interface Perfact : NSObject<APerfectDelegate> | |
@property(nonatomic, assign) id<APerfectDelegate> delegate; | |
-(void)actionWithMethodName:(NSString*)methodName; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Perface.m | |
// ReactiveCocoaPractice | |
// | |
// Created by mengqian on 20/9/2016. | |
// Copyright © 2016 thinktube. All rights reserved. | |
// | |
#import "Perface.h" | |
@implementation Perfact | |
@synthesize delegate; | |
- (instancetype)init | |
{ | |
self = [super init]; | |
if (self) { | |
// Initialize self. | |
// 需要给delegate对象指定宿主,要不无法去寻找具体的实现方法 | |
//self.delegate = self; | |
if(delegate != nil && [delegate respondsToSelector:@selector(optionalSel)]){ | |
[delegate optionalSel]; | |
} | |
} | |
return self; | |
} | |
- (void)optionalSel | |
{ | |
NSLog(@"optionalSel %@",self.name); | |
} | |
- (void)requriedSel | |
{ | |
NSLog(@"requriedSel"); | |
} | |
-(void)actionWithMethodName:(NSString*)methodName; | |
{ | |
// 默认方法 | |
if(delegate != nil && [delegate respondsToSelector:@selector(optionalSel)]){ | |
[delegate optionalSel]; | |
} | |
//通过methodName 去反射方法 | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment