I need to have a pointer array like in C, in Swift.
The following code works:
let ptr = UnsafeMutableBufferPointer<Int32>.allocate(capacity: 5)
ptr[0] = 1
ptr[1] = 5
print(ptr[0], ptr[1]) // outputs 1 5
The following code, however, does not work:
let ptr = UnsafeMutableBufferPointer<String>.allocate(capacity: 5)
print(ptr[0]) // Outputs an empty string (as expected)
print(ptr[1]) // Just exits with exit code 11
When I do print(ptr[1]) in the swift REPL, I get the following output:
Execution interrupted. Enter code to recover and continue.
Enter LLDB commands to investigate (type :help for assistance.)
How can I create a C-like array with Strings (or any other reference type, as this also doesn't seem to work with classes).
What should I adjust?
UnsafeinUnsafeMutableBufferPointerisn't coincidental.storeBytes(of:toByteOffset:as:)could be the solution instead of indexing itArray<String>and getting a pointer to the array's buffer.