【问题标题】:How to execute completition handler in Objective-C如何在 Objective-C 中执行完成处理程序
【发布时间】:2015-12-21 19:16:28
【问题描述】:

你好 stackoverflow 社区

我有来自 github 项目的以下代码: https://github.com/hybrdthry911/ELStripe 已在此处介绍: Store credit card using Stripe + Parse with cloud code

    -(void)createCardFromToken:(NSString *)tokenId customerId:(NSString *)customerId completionHandler:(ELCardCompletionBlock)handler
    {
    [ELStripe executeStripeCloudCodeWithMethod:@"POST" 
        //I use post here because we are creating a card. POST would also be used for updating a customer/card or refunding a charge for example
        prefix:@"customers" //If you look at the documentation and the example URL I use "customers" here as the prefix
        suffix:customerId //The customerID is the suffix, this will be the customer you are going to add the card to
        postfix:@"cards" //I believe this is "sources" now
        secondPostfix:nil //Not needed for this URL
        parameters:@{
            @"card":tokenId  //Only parameter is a tokenId, and I wrap this inside an NSDictionary
        }
        completionHandler:^(id jsonObject, NSError *error) {
                                 if (error)
                                 {
                                     //Handle the error code here

                                     handler(nil,error); //rejectError 
                                     return;
                                 }
                                 //If no error stripe returns a dictionary containing the card information. You can use this information to create a card object if so desired.
                                 handler([ELCard cardFromDictionary:jsonObject],error);
                             }];
    }

我现在的问题是我作为 Objective-C 中的 n00b,不知道如何使用这种方法:

[self createCardFromToken:<#(NSString *)#> customerId:<#(NSString *)#> completionHandler:<#^(ELCard *card, NSError *error)handler#>];

谁能指出我必须在之后输入的方向:completionHandler? 非常感谢!

【问题讨论】:

  • 问题不清楚。请编辑问题。
  • 既然这个问题已经有了答案,请考虑接受对你有帮助的那个。

标签: ios objective-c stripe-payments


【解决方案1】:

如果你想知道如何调用这个方法。

[self createCardFromToken:@"a token string" customerId:@"a customer id string" completionHandler:^(ELCard *card, NSError *error){
        // here goes code you want to execute when the completion handler gets called
}];

【讨论】:

    【解决方案2】:

    completionHandler 应该是一个块。你可以在这里阅读更多关于它们的信息:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html

    最简单的方法是在使用自动完成编写方法时突出显示 completionHandler 占位符时按 Enter。它会自动为您编写块骨架。如果您必须手动执行,则应如下所示:

    [self createCardFromToken:@"token value" customerId:@"customer ID" completionHandler:^(ELCard *card, NSError *error) {
        // your custom code that uses card and error values
    }];
    

    【讨论】:

    • +1 用于“突出显示占位符时按回车键”提示。这对我习惯了闭包的(起初是陌生的和令人生畏的)Swift 语法有很大帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多