我试图从 Swift native 代码返回一个 bool 值,但它没有返回 bool 值,而是返回一个字符串,其中包含我设备的 iOS 版本并向我显示错误 -
Unhandled Exception: type 'String' is not a subtype of type 'bool' in type cast
这是我的 Dart 代码导致问题的地方
static Future<bool> isLoaded() async {
return _channel.invokeMethod("isLoaded");
}
这是 iOS Swift native 代码:
case "isLoaded":
result(self.p.isContentReady) //returns a bool true or false
break;
这就是我要打印 bool 值的调用 -
print(await Tapjoy.isLoaded()) //returns iOS 14.4 as a 'String' instead of true/false
最佳答案
原来我调用它不正确,由于默认的 Flutter AppDelegate 模板,它返回了版本号 -
public class SwiftPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "flutter", binaryMessenger: registrar.messenger())
let instance = SwiftPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
result("iOS " + UIDevice.current.systemVersion)
}
}
关于ios - Swift native 代码返回 iOS 版本而不是 bool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66115861/