vDSP Cannot convert value of type '[Float]' to expected argument type 'Float'

Hi,


I just updated my macos from sierra to mojave and my xcode 9 to xcode 10.1, i am still using swift 4.0...

I am using cocoa touch framework and accelerate framework...


After the above update, the xcode has build error, i.e.

Cannot convert value of type '[Float]' to expected argument type 'Float'


The error occurs at the following code extract:

private var window:[Float]!

private var size:Int

self.size = 1000;

self.window = [Float](repeating: 0.0, count: size)

vDSP_hamm_window(&self.window!, UInt(size), 0)//error happens here


The same error message also occurs here:

private var complexBuffer: DSPSplitComplex!

private var magnitudes: [Float]!

private var halfSize:Int

private var log2Size:Int

private var fftSetup:FFTSetup

private var hasPerformedFFT: Bool = false

vDSP_fft_zrip(self.fftSetup, &(self.complexBuffer!), 1, UInt(self.log2Size), Int32(FFT_FORWARD))

self.magnitudes = [Float](repeating: 0.0, count: self.halfSize)

vDSP_zvabs(&(self.complexBuffer!), 1, &self.magnitudes!, 1, UInt(self.halfSize)) //error occurs here


May I know why this error occurs ? and how to fix this error ?


Please help


Kind Regards,

peter2013

Accepted Reply

Tested with Swift 4.2.1 on Xcode 10.1, I have confirmed your code causes the error you have described.


I do not understand what's happening inside the Swift compiler, but the following codes compile without error:

    private var window:[Float]?

        self.window = [Float](repeating: 0.0, count: size)
        vDSP_hamm_window(&self.window!, UInt(size), 0)


Or:

    private var window:[Float] = []

        self.window = [Float](repeating: 0.0, count: size)
        vDSP_hamm_window(&self.window, UInt(size), 0)

You can send a bug report, as implicitly unwrapped Optionals should work the same as usual Optionals.


But generally, you should better not use implicitly unwrapped Optionals where they are show as in your code.

- If such properties may not be uninitialized and can be nil, they should be usual Optionals.

- If they can never be nil, they should be non-Optionals.

Replies

Tested with Swift 4.2.1 on Xcode 10.1, I have confirmed your code causes the error you have described.


I do not understand what's happening inside the Swift compiler, but the following codes compile without error:

    private var window:[Float]?

        self.window = [Float](repeating: 0.0, count: size)
        vDSP_hamm_window(&self.window!, UInt(size), 0)


Or:

    private var window:[Float] = []

        self.window = [Float](repeating: 0.0, count: size)
        vDSP_hamm_window(&self.window, UInt(size), 0)

You can send a bug report, as implicitly unwrapped Optionals should work the same as usual Optionals.


But generally, you should better not use implicitly unwrapped Optionals where they are show as in your code.

- If such properties may not be uninitialized and can be nil, they should be usual Optionals.

- If they can never be nil, they should be non-Optionals.

Thanks a lot for the prompt reply and helpful explanation and sample code solution