【问题标题】:BytesArray. ObjC to Swift字节数组。 ObjC 到 Swift
【发布时间】:2015-04-27 00:20:35
【问题描述】:

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

#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 变量,但出现“架构 armv7 的未定义符号”错误。 非常感谢任何帮助。

【问题讨论】:

    标签: objective-c arrays swift byte nsdata


    【解决方案1】:

    您最大的问题是您的 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多