swiftpointersunsafemutablepointer

Extracting the real part of an UnsafeMutablePointer as a Float and storing that on an array


I have this function

func arrayForPointer<T>(_ pointer: UnsafePointer<T>, count: Int) -> [T] {
let buffer = UnsafeBufferPointer<T>(start: pointer, count: count)
return Array(buffer)
}

and this call

let arrayComplex = self.arrayForPointer(&output, count: 4)

I want to enumerate thru arrayComplex and extract the real part to a regular array, like

var arrayReal: [Float] = []

for item in arrayComplex {
  let myFloat = item.realp  \\ get the real part of item
  arrayReal.append(myFloat)
}

line

let myFloat = item.realp  \\ get the real part of item

is not correct.

item is a UnsafeMutablePointer<Float>

How do I do that, for heavens sake?

Thanks.

=======================

This is output

var A = [Float](repeating:0, count:samples/2);
var B = [Float](repeating:0, count:samples/2)
var output = DSPSplitComplex(realp: &A, imagp: &B)

Solution

  • Try this.

    Fix your func call:

    let arrayComplex = arrayForPointer(output.realp, count: 4)
    

    And then in your loop fix this line:

    let myFloat = item  \\ get the real part of item