【问题标题】:How to use FlutterStandardMessageCodec in iOS to write/read binary data?如何在 iOS 中使用 FlutterStandardMessageCodec 写入/读取二进制数据?
【发布时间】:2019-09-14 10:36:57
【问题描述】:

我可以使用这些类在 Android 和 Flutter 端使用此编解码器读取/写入二进制数据:

在 Flutter 方面:https://api.flutter.dev/flutter/services/StandardMessageCodec-class.html

Android 端:https://github.com/flutter/engine/blob/master/shell/platform/android/io/flutter/plugin/common/StandardMessageCodec.java

但我找不到如何在 iOS 端将其用作独立类,而不是在平台通道的上下文中。 我需要的只是写一些数据并获得二进制表示(例如 NSDictionary -> NSData),反之亦然。

【问题讨论】:

  • 你说的"not in context of Platform Channels"是什么意思?
  • @pskink Flutter 消息编解码器通常在平台通道中用于将数据编码/解码为二进制并返回。如果您需要以二进制格式存储一些数据并将其恢复为原始格式,我认为这是一个方便的工具。所以我想用它来达到这个目的。不与平台频道一起使用。
  • 对不起,如果没有this,如何在原生ios和dart之间交换数据?
  • @pskink 我需要通过我的服务器通过套接字交换这些数据。我的项目有这样的要求。
  • 看来你需要一个自定义的BinaryMessenger——默认的是here

标签: ios objective-c flutter


【解决方案1】:

回答我自己的问题。很简单!

        // create and fill dictionary with data
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[@"string"] = @"string value";
        dict[@"null"] = [NSNull null];
        dict[@"int"] = @(1);
        // create codec
        FlutterStandardMessageCodec *codec = [FlutterStandardMessageCodec codecWithReaderWriter:[[FlutterStandardReaderWriter alloc] init]];
        // encode to binary
        NSData *binary = [codec encode:dict];
        // decode from binary
        dict = [codec decode:binary];
        // print
        NSLog(@"Decoded value: %@", dict);

输出:

Decoded value: {
    int = 1;
    null = "<null>";
    string = "string value";
}

【讨论】:

    猜你喜欢
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    相关资源
    最近更新 更多