BytesArray。 ObjC to Swift

时间:2015-04-27 00:20:36

标签: objective-c arrays swift byte nsdata

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

func topsCollectionView(topCollectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return topImages.count
}

func bottomsCollectionView(bottomCollectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return bottomImages.count
}

我最好的假设是:

#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];

我也试图通过Bridging-Header到达ObjC变量,但是有“未定义的架构armv7符号”错误。 我真的很感激任何帮助。

1 个答案:

答案 0 :(得分:1)

您最大的问题是myCertificate数组的类型是Int not 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)

enter image description here