字节数组。 ObjC Swift

BytesArray. ObjC to Swift

我正在尝试编写 Swift 以下 ObjC(头文件)代码的实现。

#include <stddef.h>
#ifndef VO_CERTIFICATE_TYPE
#define VO_CERTIFICATE_TYPE
typedef struct _voCertificate
{
  const char* bytes;
  size_t length;
}
voCertificate;

#endif 

static const char myCertificate_BYTES[] =
{
  103,  92,   -99,  33,   72,   48,   119,  -72,  
  -77,  75,   -88,  81,   113,  -46,  -119, -119, 
  5,    42,   -33,  94,   23,   3,    -112, 34,   
  -63,  75,   -77,  26,   -41,  -69,  50,   71,   
  19,   121,  109,  -60,  40,   18,   46,   -86, 
   .......... 
};
voCertificate const myCertificate =
{
  myCertificate_BYTES,
  sizeof(myCertificate_BYTES)
};
//////////////////////////////////////
NSData *certificate = [NSData dataWithBytes:myCertificate.bytes length:myCertificate.length];

我最好的假设是:

 let myCertificate = [
          103,  92,   -99,  33,   72,   48,   119,  -72,  
          -77,  75,   -88,  81,   113,  -46,  -119, -119, 
          5,    42,   -33,  94,   23,   3,    -112, 34,   
          -63,  75,   -77,  26,   -41,  -69,  50,   71,   
          19,   121,  109,  -60,  40,   18,   46,   -86,
    ........................]

     var certificate = NSData(bytes: myCertificate as [Byte], length: myCertificate.count)

我也尝试通过 Bridging-Header 访问 ObjC 变量,但是出现 "Undefined symbols for architecture armv7" 错误。 如果有任何帮助,我将不胜感激。

你最大的问题是你的 myCertificate 数组的类型是 Int 而不是 Int8。这是对我有用的东西。请注意,我从 NSData 对象重建数组以查看是否一切正常。

let myCertificate = Array<Int8>(arrayLiteral:
    103,   92,  -99,   33,   72,   48,   119,   -72,
    -77,   75,  -88,   81,  113,  -46,  -119,  -119,
      5,   42,  -33,   94,   23,    3,  -112,    34,
     -63,  75,  -77,   26,  -41,  -69,    50,    71,
    19,   121,  109,  -60,   40,   18,    46,   -86)

var certificate = NSData(bytes: myCertificate, length: myCertificate.count)

var buffer = [Int8](count: certificate.length, repeatedValue: 0)
certificate.getBytes(&buffer, length: certificate.length)