0
所以我在Objective-C中有這個代碼,我需要重寫Swift3。從Objective-C重寫的Swift 3代碼不起作用
但是它似乎沒有工作。下面是Objective-C和下我吧重寫Swift3:
Objective-C的
- (void)handleMobilePayPaymentWithUrl:(NSURL *)url
{
[[MobilePayManager sharedInstance]handleMobilePayPaymentWithUrl:url success:^(MobilePaySuccessfulPayment * _Nullable mobilePaySuccessfulPayment) {
NSString *orderId = mobilePaySuccessfulPayment.orderId;
NSString *transactionId = mobilePaySuccessfulPayment.transactionId;
NSString *amountWithdrawnFromCard = [NSString stringWithFormat:@"%f",mobilePaySuccessfulPayment.amountWithdrawnFromCard];
NSLog(@"MobilePay purchase succeeded: Your have now paid for order with id '%@' and MobilePay transaction id '%@' and the amount withdrawn from the card is: '%@'", orderId, transactionId,amountWithdrawnFromCard);
[ViewHelper showAlertWithTitle:@"MobilePay Succeeded" message:[NSString stringWithFormat:@"You have now paid with MobilePay. Your MobilePay transactionId is '%@'", transactionId]];
} error:^(NSError * _Nonnull error) {
NSDictionary *dict = error.userInfo;
NSString *errorMessage = [dict valueForKey:NSLocalizedFailureReasonErrorKey];
NSLog(@"MobilePay purchase failed: Error code '%li' and message '%@'",(long)error.code,errorMessage);
[ViewHelper showAlertWithTitle:[NSString stringWithFormat:@"MobilePay Error %li",(long)error.code] message:errorMessage];
//TODO: show an appropriate error message to the user. Check MobilePayManager.h for a complete description of the error codes
//An example of using the MobilePayErrorCode enum
//if (error.code == MobilePayErrorCodeUpdateApp) {
// NSLog(@"You must update your MobilePay app");
//}
} cancel:^(MobilePayCancelledPayment * _Nullable mobilePayCancelledPayment) {
NSLog(@"MobilePay purchase with order id '%@' cancelled by user", mobilePayCancelledPayment.orderId);
[ViewHelper showAlertWithTitle:@"MobilePay Canceled" message:@"You cancelled the payment flow from MobilePay, please pick a fruit and try again"];
}];
}
而且我Swift3改寫:
func handleMobilePayPayment(with url: URL) {
MobilePayManager.sharedInstance().handleMobilePayPayment(with: url, success: {(mobilePaySuccessfulPayment: MobilePaySuccessfulPayment?) -> Void in
let orderId: String = mobilePaySuccessfulPayment!.orderId
let transactionId: String = mobilePaySuccessfulPayment!.transactionId
let amountWithdrawnFromCard: String = "\(mobilePaySuccessfulPayment!.amountWithdrawnFromCard)"
print("MobilePay purchase succeeded: Your have now paid for order with id \(orderId) and MobilePay transaction id \(transactionId) and the amount withdrawn from the card is: \(amountWithdrawnFromCard)")
self.alert(message: "You have now paid with MobilePay. Your MobilePay transactionId is \(transactionId)", title: "MobilePay Succeeded")
}, error: {(error: Error?) -> Void in
let dict: [AnyHashable: Any]? = error?.userInfo
let errorMessage: String? = (dict?.value(forKey: NSLocalizedFailureReasonErrorKey) as? String)
print("MobilePay purchase failed: Error code '(Int(error?.code))' and message '(errorMessage)'")
self.alert(message: errorMessage!, title: "MobilePay Error \(error?.code as! Int)")
self.alert(message: error as! String)
//TODO: show an appropriate error message to the user. Check MobilePayManager.h for a complete description of the error codes
//An example of using the MobilePayErrorCode enum
//if (error.code == MobilePayErrorCodeUpdateApp) {
// NSLog(@"You must update your MobilePay app");
//}
}, cancel: {(_ mobilePayCancelledPayment: MobilePayCancelledPayment?) -> Void in
print("MobilePay purchase with order id \(mobilePayCancelledPayment?.orderId!) cancelled by user")
self.alert(message: "You cancelled the payment flow from MobilePay, please pick a fruit and try again", title: "MobilePay Canceled")
})
}
問題的我中間出現錯誤,如下圖所示。
我不確定我在做什麼錯誤,以及如何獲得用戶信息和錯誤的錯誤信息「走出去」。
在此先感謝!
這工作!謝謝! –