Ah, NkMAIDCapInfo is a struct! That explains the problem. Consider your declaration of capArray:
var capArray: [NkMAIDCapInfo?]?
It’s an optional array of optional structs. The first optional doesn’t matter because you force unwrap it it as part of the withUnsafeMutablePointer(…) call. The second optional, however, is a big deal. C does not support optional structs (as opposed to optional pointers to structs) so this array is not layout compatible with C.
To fix this you need to make capArray an array of NkMAIDCapInfo, not NkMAIDCapInfo?. That will require you to initialise the array with something other than nil. In this situation I usually extend the struct to have an empty static member:
extension NkMAIDCapInfo {
static let empty = NkMAIDCapInfo()
}
and then populate the array like this:
capArray = [NkMAIDCapInfo](repeating: .empty, count: Int(ulCapCount))
I also suggest you audit the rest of your code for unnecessary optionality. For example, capArray doesn’t need to be an optional array because you can initialise it to an empty array.
var capArray: [NkMAIDCapInfo] = []
That change has flow-on effects, reducing the need to handle the optional value in many other places inside enumerateMAIDObjectCapabilities(…). And if you wage this War On Optional™ throughout your program, you’ll find that it’ll radically improve your Swift experience.
Finally, Swift 5.1 added a feature that let’s you initialise an array from an unsafe source without first initialising each of the elements. See SE-0245 Add an Array Initializer with Access to Uninitialized Storage for the details.
Share and Enjoy
Quinn “The Eskimo!” @ DTS @ Apple