0

Here is the simple code:

var buffer = [UInt8](_data)
var sec_ivs = [UInt8](repeating: 0, count: 8);
memcpy(&sec_ivs + 3, &buffer, 5);

The Xcode stop building the project with the following error: Generic parametwe 'Element' could not be inferred

How can I rewrite this code to make it works again in Xcode 12? This code is working fine in Xcode 11, but Xcode 11 did not support iOS 14 debuging.Thanks for help.

1 Answer 1

2

You can pass the address of the (mutable) element storage of an array to a C function with

memcpy(&sec_ivs, buffer, 5)

but that does not work with offsets. Here you need to use withUnsafeMutableBytes() to obtain a buffer pointer, so that you can add an offset:

sec_ivs.withUnsafeMutableBytes {
    memcpy($0.baseAddress! + 3, buffer, 5);
}

Note that the & operator is not needed for the second argument of memcpy() because that is a constant pointer argument.

A simpler solution would an assignment to a slice of the target array:

sec_ivs[3..<8] = buffer[0..<5]
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.