ios - Swift native 代码返回 iOS 版本而不是 bool

标签 ios swift flutter dart

我试图从 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/

相关文章:

flutter - 无状态和有状态小部件,我可以同时使用吗?

ios - Swift 中的 ENSwiftSideMenu 阴影

xcode - 如何在 iOS 中按顺序显示 Controller (作为队列,而不是堆栈)?

ios - 如何使 View 高度取决于 Swift 中的内容?

flutter - 如何将 flutter 应用程序连接到本地数据库并检索数据以制作动态 View ?

dart - 如何知道哪个屏幕在顶部(事件/安装)

ios - ld : Framework not found

ios - 使用未调用的 Apple ASAuthorizationControllerDelegate 方法登录

ios - UIImage(数据: NSData) error: Missing argument for parameter 'inBundle' in call

ios - Nib 内的 UISwitch 值未更新