The example in the Swift documentation for DenseVector_Double:
var values = [2.20, 2.85, 2.79, 2.87]
let vector = DenseVector_Double(count: Int32(values.count),
data: &values)
stride(from: 0, to: vector.count, by: 1).forEach {i in
print(vector.data[Int(i)])
}
worked fine previously (prior to 5.2 I believe).
It now has a warning:
"Inout expression creates a temporary pointer, but argument 'data' should be a pointer that outlives the call to 'init(count:data:)'"
What would need to be done to fix the example and avoid the error message?
Yeah, as Claude31 says, the documentation is definitely wrong here.
DenseVector_Double
is a C structure containing a pointer and a count. When you set up a structure like this, you must ensure that the pointer is valid for the lifetime of the structure. Using
&
like this creates a pointer that only lasts for the duration of the call, which in this case is the structure’s initialiser.
The best way to fix this depends on how you’re using the data. For this simple example, you could write this:
var values = [2.20, 2.85, 2.79, 2.87]
values.withUnsafeMutableBufferPointer { buf in
let vector = DenseVector_Double(
count: Int32(buf.count),
data: buf.baseAddress!
)
stride(from: 0, to: Int(vector.count), by: 1).forEach { i in
print(vector.data[i])
}
}
However, it’s likely that any real code would need something more complex.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"