如何将 Swift Promise 桥接到 React Native
How to bridge a Swift Promise to React Native
我正在将 iOS 本机 SDK 集成到 React-Native 中。我想从 RN 使用一个名为 SDK.getCardData
的函数。我的第一次尝试是在闭包内调用 resolve
和 reject
:
import Foundation
import SDK
@objc(SwiftComponentManager)
class SwiftComponentManager: NSObject {
@objc
func getCardData(_ resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) -> Void {
let cardId: String = "test"
let secret: String = "test"
SDK.getCardData(cardId, secret: secret) { (cardData, error) in
if (error != nil) {
reject(String(format: "Card data request failed: %@", error!.localizedDescription))
} else {
let pan = cardData!.pan
let cvv = cardData!.cvv
resolve(String(format: "Card data fetched successfully, pan: %@, cvv: %@", pan, cvv))
}
}
}
@objc func testMethod() -> Void {
print("This Does appear")
}
}
不幸的是,这会引发以下错误:escaping closure captures non-escaping parameter 'resolve'
。
第二次尝试:
import Foundation
import SDK
import Promises
@objc(SwiftComponentManager)
class SwiftComponentManager: NSObject {
@objc
func getCardData(_ resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) -> Promise<CardData?> {
let cardId: String = "test"
let secret: String = "test"
let promise = Promise<CardData?>()
SDK.getCardData(cardId, secret: secret) { (cardData, error) in
if (error != nil) {
promise.reject(error)
} else {
let pan = cardData!.pan
let cvv = cardData!.cvv
promise.resolve(cardData)
}
}
return promise
}
@objc func testMethod() -> Void {
print("This Does appear")
}
}
如何正确调用resolve
& reject
? return Void
, .
的功能很重要
找到 answer,只需将 @escaping 添加到参数中:
@objc func fling(_ options: NSDictionary, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) -> Void {
...
我正在将 iOS 本机 SDK 集成到 React-Native 中。我想从 RN 使用一个名为 SDK.getCardData
的函数。我的第一次尝试是在闭包内调用 resolve
和 reject
:
import Foundation
import SDK
@objc(SwiftComponentManager)
class SwiftComponentManager: NSObject {
@objc
func getCardData(_ resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) -> Void {
let cardId: String = "test"
let secret: String = "test"
SDK.getCardData(cardId, secret: secret) { (cardData, error) in
if (error != nil) {
reject(String(format: "Card data request failed: %@", error!.localizedDescription))
} else {
let pan = cardData!.pan
let cvv = cardData!.cvv
resolve(String(format: "Card data fetched successfully, pan: %@, cvv: %@", pan, cvv))
}
}
}
@objc func testMethod() -> Void {
print("This Does appear")
}
}
不幸的是,这会引发以下错误:escaping closure captures non-escaping parameter 'resolve'
。
第二次尝试:
import Foundation
import SDK
import Promises
@objc(SwiftComponentManager)
class SwiftComponentManager: NSObject {
@objc
func getCardData(_ resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) -> Promise<CardData?> {
let cardId: String = "test"
let secret: String = "test"
let promise = Promise<CardData?>()
SDK.getCardData(cardId, secret: secret) { (cardData, error) in
if (error != nil) {
promise.reject(error)
} else {
let pan = cardData!.pan
let cvv = cardData!.cvv
promise.resolve(cardData)
}
}
return promise
}
@objc func testMethod() -> Void {
print("This Does appear")
}
}
如何正确调用resolve
& reject
? return Void
,
找到 answer,只需将 @escaping 添加到参数中:
@objc func fling(_ options: NSDictionary, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) -> Void {
...