0

For performance reasons I need drop into C for my inner loop. I'm trying to debug it and want to be able to store a string in a C struct.

typedef struct Slot {
    char name[12];
    Byte fixed;
    Byte loaded;
    Obj obj;
} Slot;

At the moment this is just for debugging purposes, so I'm not too particular about how to make this happen (i.e., char* name? char name[12]? something else?)

On the swift side this is literally the best I've been able to come up with so far:

var vars = [String]()
...
var i = 0
for v in vars {
    let array: [UInt8] = Array(v.utf8)

    memory.pointee.slots[i].name.0 = Int8(array[0])
    memory.pointee.slots[i].name.1 = Int8(array[1])
    memory.pointee.slots[i].name.2 = Int8(array[2])
    memory.pointee.slots[i].name.3 = Int8(array[3])
    memory.pointee.slots[i].name.4 = Int8(array[4])
    memory.pointee.slots[i].name.5 = Int8(array[5])
    memory.pointee.slots[i].name.6 = Int8(array[6])
    memory.pointee.slots[i].name.7 = Int8(array[7])
    memory.pointee.slots[i].name.8 = Int8(array[8])
    memory.pointee.slots[i].name.9 = Int8(array[9])
    memory.pointee.slots[i].name.10 = Int8(array[10])
    memory.pointee.slots[i].name.11 = Int8(array[11])
}

which doesn't work if the String is less than 12 characters. (And is ridiculous anyway)

How is one supposed to do this?

Edit: Not duplicate of Obtain a pointer to a C char array in Swift because they are trying to go from C to Swift not Swift to C and because I'm not necessarily restricted by a fix length char array, that was merely the "best" solution I've found so far.

Edit 2: This IS a duplicate of Convert an String to an array of int8. Which solved my problem (with the recent update). Thanks.

Community
  • 1
  • 1
aepryus
  • 4,715
  • 5
  • 28
  • 41
  • Where is your original Swift `String` value? What is `array` and `v.utf8`? Are they raw byte arrays, Swift character arrays, `CChar` arrays or what? – Dai Feb 05 '17 at 06:14
  • @Dai vars is a String array, i.e., [String](). v is therefore just a String instance. Array is a UInt8 array: [UInt8] and v.utf8 is a String.UTF8View according to Xcode. – aepryus Feb 05 '17 at 06:22

1 Answers1

0

It looks like you want the getCString method from Swift's String type: https://developer.apple.com/reference/swift/string/1413114-getcstring

In your case, I'm guessing this (you didn't say which variable is your Swift String).

 var str: String = someStringValue

 let nameStart = &memory.pointee.slots[i].name // pointer to the start of the `name` buffer

 str.getCString( nameStart, maxLength: 12 )

(Note I'm not a regular Swift user, and I'm still not up-to-date on Swift 3, so this code probably won't compile, but I hope you get the jist of it)

Dai
  • 141,631
  • 28
  • 261
  • 374
  • "Cannot convert value of type '(Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8)' to expected argument type '[CChar]'" – aepryus Feb 05 '17 at 06:19
  • @aepryus Oh, I see - Swift has interpreted `name[12]` as a 12-tuple instead of an array value. – Dai Feb 05 '17 at 06:36