NativeScript:从 interop.reference 获取字符串

NativeScript: Get string from interop.reference

首先,这是我的代码:

var buffer = malloc(interop.sizeof(interop.types.UTF8CString));
var fillBuffer = mac.getBytes(buffer);
var bytes = new interop.Reference(interop.types.UTF8CString, buffer);
var hexMac = bytes[0];

变量'Mac'是一个从CoreBluetooth检索到的NSData对象。它是来自 BLE 设备的扫描响应,其中包含外设的 MAC 地址 (00:0b:57:a2:fb:a0).

此问题与我之前发布的 问题有关。

提供的解决方案很棒;但是,我似乎无法在 nativescript 中实现它:

(实例类型)stringWithFormat:(NSString *)格式, ...;

Intellisense 告诉我该方法在 NSString 类型上不存在。

由于这个问题,我决定走另一条路(如您所知)。我正在用 MAC 地址的字节填充缓冲区。在上面的代码中,bytes[0] 等于 0xb57a2fba0。

我现在正在尝试将它(它是一个 interop.Reference)转换成一个我可以存储在后端的字符串(最好是 xx:xx:xx:xx:xx 格式)。

我整个周末都在这个问题上,似乎找不到解决办法。我什至分解了 objc!foundation.d.ts 来弄清楚是否支持 stringWithFormat,但无济于事。

nativescript 社区 slack 也无法提供解决方案。

如果可以请帮忙!

我对 NativeScript 一无所知,但考虑到您编写的其他代码,我假设您首先调用 +alloc,因此打算使用 -initWithFormat:(一个实例初始化的方法)而不是 +stringWithFormat:(处理分配和初始化的 class 方法)。