Array cannot be inferred Error in Xcode 12 with Swift 5

142 views Asked by At

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

There are 1 answers

0
Martin R On BEST ANSWER

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]