如何在Objective-C中执行completition处理程序

时间:2015-09-24 08:45:28

标签: ios objective-c stripe-payments

您好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#>];

有没有人能指出我的方向:完成握手? 非常感谢你!

2 个答案:

答案 0 :(得分:2)

如果您想知道如何调用此方法。

[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
}];

答案 1 :(得分: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
}];