0

I have a C library with following struct

typedef struct Client_
{
    /*! Display Name for Guest only */
    char displayName[USERID_SIZE];
    /*! Conference PIN if needed */
    char pin[PIN_SIZE];
} Client;

From swift, I have to assign the members with string.

var client = Client();
let guestVName = "Swift user";
let guestVRoomPin = "";

How to do this? Please help.

Bobby
  • 141
  • 11
  • 1
    Something like this http://stackoverflow.com/questions/27461904/convert-an-string-to-an-array-of-int8 ? (It probably needs an update for Swift 2 or Swift 3.) – Martin R Sep 20 '16 at 12:08

1 Answers1

1

You may need to write something like this:

strlcpy(&client.displayName.0, guestVName, Int(USERID_SIZE))
strlcpy(&client.pin.0, guestVRoomPin, Int(PIN_SIZE))

In C-language, arrays are passed by the pointer to the first element, and in Swift, fixed sized C-arrays are imported as tuples. So, you need to emulate what C-compiler does. In the code above, you can pass the pointer to the first element of the tuple.

(Remember, strlcpy is not a Standard C-library function. And it cuts off the exceeded part of the string, which may generate an invalid byte sequence as UTF-8.)

OOPer
  • 47,149
  • 6
  • 107
  • 142