如何引用类的方法 [英] How to reference a method of a class

查看:98
本文介绍了如何引用类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个App.net客户端,并使用ADNKit作为与ADN服务器通信的框架。我从 Zephyr 的开源代码中获得了一些想法,该app.net客户端已被关闭

I'm writing an App.net client and using ADNKit as the framework that communicates with the ADN's servers. I've gotten a few ideas from the open source code of Zephyr, an app.net client that was turned into an open source project.

其用于显示帖子列表的视图控制器(PostStreamViewController)使用数据控制对象和配置对象,该对象具有名为<$ c $的属性c> apiCallMaker 。

Their view controllers for displaying lists of posts (PostStreamViewController) use a data controlling object and a configuration object that has a property called apiCallMaker.

该属性的定义如下:

typedef void (^APIPostListCallback)(NSArray *posts, PostListMetadata *meta, NSError *error);
...
@property (nonatomic, copy) void (^apiCallMaker)(APIPostParameters *parameters, APIPostListCallback callback);

这使他们可以将相同的数据控制器对象重用于主时间轴,提及时间轴等。他们需要做的是为每种这些帖子流类型提供一个配置文件,每种引用不同的api调用。

This allows them to reuse the same data controller object for the main timeline, mentions timeline, etc. All they need to do is provide a configuration file for each of these post stream types, each one referencing a different api call.

例如,在提及帖子流的配置文件中,他们定义self.apiCallMaker像这样:

For example, in the configuration file for the mentions post stream they define self.apiCallMaker like this:

- (void (^)(APIPostParameters *parameters, APIPostListCallback callback))apiCallMaker
{
    return [^(APIPostParameters *parameters, APIPostListCallback callback) {
        [APIUserMentionStream getUserMentionStreamWithParameters:parameters userID:self.userID completionHandler:callback];
    } copy];
}

这变得long不休,但请坚持。我认为这种方法很酷,它使数据控制器对象和配置文件更轻便。

This is becoming long winded but stick with me. I thought this method was quite cool, it makes for a lighter data controller object and configuration files are easy and light.

在我的实现中,我声明了 apiCallMaker 像这样:

In my implementation, I instead declare my apiCallMaker like this:

// this is the default parameters returned by ANKClients post fetching methods
typedef void (^APIPostListCallback)(id responseObject, ANKAPIResponseMeta *meta, NSError *error);  
... 
@property (nonatomic, copy) void (^apiCallMaker)(ANKClient *client, APIPostListCallback callback);

在我的配置文件中,我定义了 apiCallMaker

In my configuration files I define apiCallMaker like this:

- (void (^)(ANKClient *client, APIPostListCallback callback))apiCallMaker
{
    return [^(ANKClient *client, APIPostListCallback callback) {
        [client fetchPostsMentioningUser:self.user completion:callback];
    } copy];
}

然后在获取帖子时我会这样做:

Then when fetching posts I do this:

ANKClient *client = ... // authenticated client object with parameters
self.apiCallMaker(clientCopy, ^(id responseObject, ANKAPIResponseMeta *meta, NSError *error){
    if (!error) {
        // handle data
    } else {
        // handle error
    }
});

此问题是我无法存储对的引用当我通过 [client fetchPostsMentioningUser:self.usercomplete:callback]; 时,由ANKClient对象返回的ANKJSONRequestOperation > apiCallMaker 。我想存储对 ANKJSONRequestOperation 的引用,因为当我的视图控制器弹出/取消分配时,我可以轻松地取消网络请求。

The problem with this is that I can't store a reference to the ANKJSONRequestOperation that is returned by the ANKClient object when I call [client fetchPostsMentioningUser:self.user completion:callback]; via the apiCallMaker. I want to store the reference to the ANKJSONRequestOperation because I can easily cancel network requests when my view controller is popped/deallocated.

- (void)fetchPosts
{
    ANKClient *client = ... // authenticated client object with parameters
    self.requestOperation = [clientCopy fetchPostsMentioningUser:user completion:^(id responseObject, ANKAPIResponseMeta *meta, NSError *error) {
        // handle posts/error
    }];
}

...

- (void)dealloc {
    ...
    [self.requestOperation cancel];
}

有没有一种方法可以存储对我需要调用的方法的引用( fetchPostsMentioningUser:)同时仍在调用客户端对象,以便我可以存储返回的 ANKJSONRequestOperation

Is there a way to store a reference to the method that I need to call (fetchPostsMentioningUser:) while still calling the client object so I can store the returned ANKJSONRequestOperation?

好,按照@berg的建议,我已更改了物业的返回类型现在为 ANKJSONRequestOperation *

Ok, as suggested by @berg, I've changed my property's return type to now be ANKJSONRequestOperation *.

我忘了提到我在数据控制器上也有一个属性,就像配置文件一样。这有点多余,但是在初始化数据控制器时,我使用配置中的apiCallMaker对其进行了设置。所以这是我现在的布局方式,我更改了测试的属性名称。

I forgot to mention that I also have a property on the data controller that is just like the configuration file. It's kind of redundant but when initializing the data controller I set its apiCallMaker with the one from the configuration. So here's how I have it laid out now, I've changed the property name for testing.

问题是,每当我设置self.fetcher时,它就是 NULL 。我是否需要更改配置文件返回ANKJSONRequestOperation的方式。请原谅我的无知,积木让我头疼很多

The problem is, whenever I set self.fetcher it is NULL. Do I need to change how the configuration file returns the ANKJSONRequestOperation. Please forgive my ignorance, blocks give me so many headaches.

typedef void (^APIPostListCallback)(id responseObject, ANKAPIResponseMeta *meta, NSError *error);
@property (nonatomic, readonly) ANKJSONRequestOperation *(^fetcher)(ANKClient *client, APIPostListCallback callback);



Configuration.m

Configuration.m

- (ANKJSONRequestOperation *(^)(ANKClient *client, APIPostListCallback callback))fetcher
{
    return [^(ANKClient *client, APIPostListCallback callback) {
        [client fetchUnifiedStreamForCurrentUserWithCompletion:callback];
    } copy];
}



DataController.h

DataController.h

@property (nonatomic, copy) ANKJSONRequestOperation *(^fetcher)(ANKClient *client, APIPostListCallback callback);



DataController.m

DataController.m

@property (nonatomic, strong) ANKJSONRequestOperation *operation;

- (id)initWithConfiguration:(Configuration *)configuration {
    ...
    self.fetcher = configuration.fetcher
}

- (void)fetchPosts {
    ...
    self.operation = self.fetcher(clientCopy, ^(id responseObject, ANKAPIResponseMeta *meta, NSError *error) {
        if (!error) {
            [self.data setPosts:responseObject meta:meta];
        } else {
            // handle error
        }
    });
}


推荐答案

属性获取器的实现在Configuration.m中应该是:

The implementation of your property getter in Configuration.m should be:

- (ANKJSONRequestOperation *(^)(ANKClient *client, APIPostListCallback callback))fetcher
{
    return [^(ANKClient *client, APIPostListCallback callback) {
        return [client fetchUnifiedStreamForCurrentUserWithCompletion:callback];
    } copy];
}

这篇关于如何引用类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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