解决“单接口,多类”架构设计的回调问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
cralison
V2EX    iDev

解决“单接口,多类”架构设计的回调问题

  •  
  •   cralison 2015-09-07 01:59:29 +08:00 1618 次点击
    这是一个创建于 3765 天前的主题,其中的信息可能已经有所发展或是发生改变。

    全力推动信息,而非照顾他人感受

    最终效果:

    alt text

    alt text

    工程目录:

    alt text

    代码:

    APIInterface.h

    #ifndef CallBack_APIInterface_h #define CallBack_APIInterface_h @protocol APIInterface <NSObject> - (void )loadData; @end #endif 

    APIParamSourceInterface.h

    #import "APIInterface.h" #ifndef CallBack_APIParamSourceInterface_h #define CallBack_APIParamSourceInterface_h @protocol APIParamSourceInterface <NSObject> - (NSDictionary *)paramSourceAPIHandle:(id<APIInterface>)apiHandle; @end #endif 

    APICallbackInterface.h

    #import "APIInterface.h" #ifndef CallBack_APICallbackInterface_h #define CallBack_APICallbackInterface_h @protocol APICallbackInterface <NSObject> - (void )listWtihData:(NSArray *)data apiHandle:(id<APIInterface>)apiHandle; @end #endif 

    APIHandle.h

    #import <Foundation/Foundation.h> #import "APIInterface.h" #import "APIParamSourceInterface.h" #import "APICallbackInterface.h" @interface APIHandle : NSObject<APIInterface> @property (nonatomic, strong ) NSMutableData *receiveData; @property (nonatomic, weak ) id<APIParamSourceInterface> paramSourceHandle; @property (nonatomic, weak ) id<APICallbackInterface> callbackHandle; @end 

    APIHandle.m

    #import "APIHandle.h" @implementation APIHandle - (void )loadData { self.receiveData = [[NSMutableData alloc]init]; NSDictionary *paramSource = [self.paramSourceHandle paramSourceAPIHandle:self]; NSURL *url = [[NSURL alloc]initWithString:paramSource[@"URLString"]]; NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url]; NSURLConnection *cOnnection= [[NSURLConnection alloc]initWithRequest:request delegate:self]; } - (void )connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.receiveData appendData:data]; } - (void )connectionDidFinishLoading:(NSURLConnection *)connection { NSError *error = nil; id data = [NSJSONSerialization JSONObjectWithData:self.receiveData options:NSJSONReadingAllowFragments error:&error]; NSLog (@"%@ data\n%@",self,data ); [self.callbackHandle listWtihData:data apiHandle:self]; } @end 

    HotAPIHandle.h

    #import "APIHandle.h" @interface HotAPIHandle : APIHandle @end 

    HotAPIHandle.m

    #import "HotAPIHandle.h" @implementation HotAPIHandle @end 

    LatestAPIHandle.h

    #import "APIHandle.h" @interface LatestAPIHandle : APIHandle @end 

    LatestAPIHandle.m

    #import "LatestAPIHandle.h" @implementation LatestAPIHandle @end 

    ViewController.h

    #import <UIKit/UIKit.h> #import "APIInterface.h" #import "APIParamSourceInterface.h" #import "APICallbackInterface.h" @interface ViewController : UIViewController<APIParamSourceInterface,APICallbackInterface> @property (nonatomic, weak ) id<APIInterface> hotAPIHandel; @property (nonatomic, weak ) id<APIInterface> latestAPIHandel; @end 

    ViewController.m

    #import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> @property (nonatomic, strong ) NSArray *list; @property (nonatomic, strong ) UITableView *tableView; @end @implementation ViewController - (void )viewDidLoad { [super viewDidLoad]; [self initUI]; [self.hotAPIHandel loadData]; } -(void )initUI { self.title = @"最热-最新主题"; CGRect frame = [[UIScreen mainScreen]bounds]; self.tableView = [[UITableView alloc]initWithFrame:frame]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.view addSubview:self.tableView]; } - (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger )section { return self.list.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"cellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell ) { cell = [[UITableViewCel alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier]; } NSDictionary * data =self.list[indexPath.row]; return [self configureWithCell:cell data:data]; } - (UITableViewCell *)configureWithCell:(UITableViewCell *)cell data:(NSDictionary *)data { cell.textLabel.text = data[@"title"]; cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http:%@",data[@"member"][@"avatar_mini"]]]]]; return cell; } -(NSDictionary *)paramSourceAPIHandle:(id<APIInterface>)apiHandle { if ([apiHandle isKindOfClass:[self.hotAPIHandel class]]) { return @{ @"URLString" : @"api/topics/hot.json" }; } else if ([apiHandle isKindOfClass:[self.latestAPIHandel class]]) { return @{ @"URLString" : @"api/topics/latest.json" }; } return nil; } - (void )listWtihData:(NSArray *)data apiHandle:(id<APIInterface>)apiHandle { if ([apiHandle isKindOfClass:[self.hotAPIHandel class]]) { self.list = data; [self.latestAPIHandel loadData]; } else if ([apiHandle isKindOfClass:[self.latestAPIHandel class]]) { self.list = data; } [self tableViewReloadData]; } - (void )tableViewReloadData { [self.tableView reloadData]; } - (void )didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end 

    AppDelegate.m

    #import "HotAPIHandle.h" #import "LatestAPIHandle.h" @interface AppDelegate () @property (nonatomic, strong ) HotAPIHandle *hotAPIHandle; @property (nonatomic, strong ) LatestAPIHandle *latestAPIHandle; @end @implementation AppDelegate - (BOOL )application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; ViewController *viewCOntroller= [[ViewController alloc] init]; viewController.hotAPIHandel = self.hotAPIHandle; self.hotAPIHandle.callbackHandle = viewController; self.hotAPIHandle.paramSourceHandle = viewController; viewController.latestAPIHandel = self.latestAPIHandle; self.latestAPIHandle.callbackHandle = viewController; self.latestAPIHandle.paramSourceHandle = viewController; UINavigationController *navigatiOnController= [[UINavigationController alloc]initWithRootViewController:viewController]; self.window.rootViewCOntroller= navigationController; [self.window makeKeyAndVisible]; return YES; } - (HotAPIHandle *)hotAPIHandle { if (!_hotAPIHandle ) { _hotAPIHandle = [[HotAPIHandle alloc] init]; } return _hotAPIHandle; } - (LatestAPIHandle *)latestAPIHandle { if (!_latestAPIHandle ) { _latestAPIHandle = [[LatestAPIHandle alloc] init]; } return _latestAPIHandle; } 

    很多朋友问我,这样设计的优势在哪里?
    我觉得最大的优势就是:你很容易找到出错的代码。

    后记(下面以聊家常为主,没时间没兴趣的朋友请直接忽略):

    我思考过很多次,要不要继续全力推动信息。
    我当然知道,因为我的水平有限,经验有限,智商有限,在大多数情况下,我推动的信息是错的。
    但是,从我的人生经历来看,全力推,还是会更有益,更划算。

    我们很容易看到一些“不全力推”的例子。
    比如,有人会说“我今天收获很大”,然后就没有然后了,或者需要等到有人请教他,他才会继续说下去。
    比如,有人会说“你这样是错的”,然后就没有然后了,或者需要等到有人请教他,他才会继续说下去。

    我当然知道每个信息都得来不易。
    我当然知道信息差可以产生优势。
    我只是更清楚,全力去推,即使会得罪一些人,最终还是会收获更多。

    退一步讲,即使因此而失去再多的机会,我好像也不在乎。因为,对我而言,推动信息才是我一生最想要的。

    推它个天翻地覆。

    目前尚无回复
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2470 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 04:29 PVG 12:29 LAX 20:29 JFK 23:29
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86