Objective-C回调/阻止模式 [英] Objective-C Callbacks/Block Pattern

查看:89
本文介绍了Objective-C回调/阻止模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是从远程服务器加载人员列表(JSON格式),将文件保存到磁盘上,然后解析结果并将NSArray *返回给调用者.

What I am trying to do is load a list of people (JSON format) from a remote server, save the file onto disk, and then parse the result and return an NSArray * back to the caller.

我创建了一个具有以下内容的EmployeeDirectoryManager:

I have created a EmployeeDirectoryManager that has the following:

- (NSArray *)loadDirectory:(BOOL)refreshFromServer;
- (void)loadDirectoryFromFile;
- (void)loadDirectoryFromServer;

我想在loadDirectory方法上使用一个块,以便在loadDirectoryFromServer(正在使用带有success块的AFJSONRequestOperation)时通知调用者.

I would like to use a block on the loadDirectory method so the caller can be informed when the loadDirectoryFromServer, which is using an AFJSONRequestOperation which has a success block on it.

关于如何实现这一目标,或者如果我走错了路,我需要一些指导.

I need a little direction on how to implement this, or if I am headed down the wrong path.

推荐答案

要将方法中的块用作完成处理程序,首先需要定义新类型

To use block in your methods as completion handler, firstly you need define new type

typedef void(^TypeComplitionHandler)(id result)

然后,您可以将块传递给您的方法.例如

Then you can pass block to your method. For example

- (void)loadDirectoryFromFileComplitionHandler:(TypeComplitionHandler)complition {
    complition(@"done");
}

这篇关于Objective-C回调/阻止模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆