Objective-C Callbacks/Block Pattern

走远了吗. 提交于 2019-12-10 11:38:06

问题


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.

I have created a EmployeeDirectoryManager that has the following:

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

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.


回答1:


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");
}


来源:https://stackoverflow.com/questions/14738667/objective-c-callbacks-block-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!