objective-c - PromiseKit 框架 - 对成员 'then()' 的模糊引用

标签 objective-c swift promise promisekit

我正在使用 PromiseKit 框架版本 1.7.7(我需要使用这个版本,因为另一个框架需要它)。

所以,在这个使用PromiseKit框架的框架中有这个方法:

- (PMKPromise *)paymentTokenForCreditCard:(GNCreditCard *)creditCard {
    NSDictionary *cardDict = [creditCard paramsDicionary];
    NSString *jsonCard = [self getJSONStringFromDictionary:cardDict];

    return [self encryptData:jsonCard]
    .then(^(NSString *encryptedData){
        NSDictionary *params = @{@"data":encryptedData};
        return [self request:kGNApiRouteSaveCard method:@"POST" params:params];
    })
    .then(^(NSDictionary *response){
        return [[GNPaymentToken alloc] initWithDictionary:response];
    });
}

它展示了如何使用它的例子:

GNConfig *gnConfig = [[GNConfig alloc] initWithAccountCode:@"YOUR_ACCOUNT_CODE" sandbox:YES];

GNApiEndpoints *gnApi = [[GNApiEndpoints alloc] initWithConfig:gnConfig];

GNCreditCard *creditCard = [[GNCreditCard alloc] init];
creditCard.number = @"4012001038443335";
creditCard.brand = kGNMethodBrandVisa;
creditCard.expirationMonth = @"05";
creditCard.expirationYear = @"2018";
creditCard.cvv = @"123";

[gnApi paymentTokenForCreditCard:creditCard]
.then(^(GNPaymentToken *paymentToken){
NSLog(@"%@", paymentToken.token);
})
.catch(^(GNError *error){
NSLog(@"An error occurred: %@", error.message);
});

好吧,我是如何使用 Swift 而不是 Object-C 的,我正在尝试以这种方式使用它:

let gnConfig = GNConfig(accountCode: "3f62976bea79971730b67cd62806c256", sandbox: true)
let gnEndpoints = GNApiEndpoints(config: gnConfig)

let gnCreditCard: GNCreditCard! = GNCreditCard(number: "4012001038443335", brand: kGNMethodBrandVisa, expirationMonth: "05", expirationYear: "2018", cvv: "123")

gnEndpoints?.paymentToken(for: gnCreditCard).then({ tokenPagamento in
    if let aToken = tokenPagamento?.token {
        print("\(aToken)")
    }
}).catch({ error in
    if let aMessage = error?.message {
        print("An error occurred: \(aMessage)")
    }
})

它向我显示了这个错误:

Ambiguous reference to member 'then()'

我该如何解决?

最佳答案

试试下面的代码。 promise 的事情应该有效。根据您的需要修改它。

gnEndpoints?.paymentToken(for: gnCreditCard).then({ token -> () in
   if let token = token as? GNPaymentToken {
      print(token)
   }
}, { (error) -> () in
   // catch your error
})

关于objective-c - PromiseKit 框架 - 对成员 'then()' 的模糊引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50747469/

相关文章:

promise - 如何从嵌套的 kotlin.js.Promise 创建 Promise?

objective-c - 将文本文件合并到我的应用程序的构建中

objective-c - MCNearbyServiceAdvertiser 与 MCAdvertiserAssistant

swift - 如何在 Mac OS 上截屏 NSView?

ios - Swift - didSet 属性未从绑定(bind)更改中调用

javascript - Q 返回已解决的 promise ?

ios - UIView动画警告

ios - 消除键盘 FirstResponder 问题

ios - 无法从 AppDelegate 中实例化 rootViewController

javascript - Ember 框架中使用的 promise 实现