首页 > 解决方案 > 如何在 Objective-C 中将完成处理程序分配给另一个

问题描述

我在完成处理程序中有一个问题。

我有一个方法如下

-(void)authenticationRealmWithCompletion:(void (^)(AuthenticationRealm *authenticationRealm, NSError *error))completion realmId:(NSString *)realmId postDictionary:(NSDictionary *)postDictionary{
//my code
    }

现在我声明了一个变量如下

typedef void(^completionHandler)(AuthenticationRealm *authenticationRealm, NSError *error);

现在我想分配如下

-(void)authenticationRealmWithCompletion:(void (^)(AuthenticationRealm *authenticationRealm, NSError *error))completion realmId:(NSString *)realmId postDictionary:(NSDictionary *)postDictionary{
[completion copy];

//**HERE**..... Error Line
completionHandler(completion);   

//my code



 }

但我收到错误Redefinition of 'completion'

请建议如何将完成处理程序分配给另一个。

标签: iosobjective-ciphone

解决方案


要分配完成处理程序,您必须创建完成处理程序的属性。

@property (nonatomic, copy) completionHandler completion;

然后在您的方法中使用以下代码进行分配。

self.completion = completion;

更新

方法也可以定义为

-(void)authenticationRealmWithCompletion:(completionHandler)completion
                             realmId:(NSString *)realmId
                      postDictionary:(NSDictionary *)postDictionary{

self.completion = completion;

}

推荐阅读