代码之家  ›  专栏  ›  技术社区  ›  Gagan_iOS

如何在Objective-C中将完成处理程序分配给其他处理程序

  •  0
  • Gagan_iOS  · 技术社区  · 8 年前

    我在完成处理程序中遇到问题。

    我有一个方法如下

    -(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'

    1 回复  |  直到 8 年前
        1
  •  3
  •   piet.t Charis A.    8 年前

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

    @property (nonatomic, copy) completionHandler completion;
    

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

    self.completion = completion;
    

    -(void)authenticationRealmWithCompletion:(completionHandler)completion
                                 realmId:(NSString *)realmId
                          postDictionary:(NSDictionary *)postDictionary{
    
    self.completion = completion;
    
    }