首页 > 解决方案 > Swift 中的 ArrayBuffer 和 Float64Array

问题描述

我正在尝试将其中一个 javascript 函数转换为 Swift 5 函数。但即使经过如此广泛的搜索,我也找不到任何东西。

function toArrayBuffer(type, ts, x, y, z, sc) {
  let userId = userID.getUserId();
  //console.log("Found GPS UserID: " + userId);
  if (userId == "Unauthor") {
    //console.log("GPS did not find the userID")
    return null;
  }

  let buffer = new ArrayBuffer(8 * 4);

  // Offset: 0
  let float64Bytes = new Float64Array(buffer);
  float64Bytes[0] = ts;

  // Offset: 8
  let bytes = new Float32Array(buffer);
  bytes[2] = x;
  bytes[3] = y;
  bytes[4] = z;
  bytes[5] = sc;

  // Offset: 24
  let uint8Bytes = new Uint8Array(buffer);
  uint8Bytes[24] = type;

  for (let i = 0; i < 8; i++) {
    if (userId.charCodeAt(i)) {
      uint8Bytes[i + 25] = userId.charCodeAt(i);
    }
  }

  return buffer;
}

基本上我尝试构建相同的功能,var byteArray = [UInt8](stringVal.utf8)UInt8最多只能存储 256 个,但我必须存储纪元时间戳。所以,它也不起作用。任何帮助,将不胜感激。

标签: javascriptiosarraysswiftbyte

解决方案


推荐阅读