ios - 如何在 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'
请建议如何将完成处理程序分配给另一个。
解决方案
要分配完成处理程序,您必须创建完成处理程序的属性。
@property (nonatomic, copy) completionHandler completion;
然后在您的方法中使用以下代码进行分配。
self.completion = completion;
更新
方法也可以定义为
-(void)authenticationRealmWithCompletion:(completionHandler)completion
realmId:(NSString *)realmId
postDictionary:(NSDictionary *)postDictionary{
self.completion = completion;
}
推荐阅读
- ionic3 - ionic3 文件/图像上传仅适用于模拟器而不适用于 apk
- ios - 在重叠区域点击手势事件
- css - 如何阻止角材料组件与我的粘性导航栏重叠?
- python-3.x - Mac OS 上的 pgmagick
- c++ - 如何退出我的 do while 循环?C++
- javascript - Highstock(Highcharts):自动显示工具提示值,以百万/十亿等为单位
- batch-file - 从日志文件批处理中的一行中提取部分字符串
- java - ClassNotFoundException:org.flywaydb.core.api.callback.FlywayCallback
- bitbucket - 使用 api 2.0 列出 bitbucket 的私有存储库
- sql - 从完成的过程中将数据插入表中