Obtain a pointer to a C char array in Swift

后端 未结 3 959
故里飘歌
故里飘歌 2021-01-06 00:19

A have a structure like this (defined in bson.h of mongodb c driver):

typedef struct
{
  uint32_t domain;
  uint32_t code;
  char message[504];
} bson_error_         


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-06 00:51

    Here my suggestion (similar to rintaro's approach, perhaps slightly simpler):

    var err: UnsafeMutablePointer = ...
    
    var msg = err.memory.message
    let msgString = withUnsafePointer(&msg) { String.fromCString(UnsafePointer($0)) }
    println(msgString)
    

提交回复
热议问题