I am trying to learn how to create a FFT using swift 2.0 however i am having trouble getting the .map function to compile.
The following code works in a playground but not inside xCode as a member of a swift class.
I get the following error "Instance member 'sineArraySize' cannot be used on type 'FFTAnalyser'
import Foundation
import Accelerate
class FFTAnalyser {
let sineArraySize = 64 // Should be power of two for the FFT
let frequency1 = 4.0
let phase1 = 0.0
let amplitude1 = 2.0
var sineWave = (0..<sineArraySize).map {
amplitude1 * sin(2.0 * M_PI / Double(sineArraySize) * Double($0) * frequency1 + phase1)
}
func plotArray<T>(arrayToPlot:Array<T>) {
for x in arrayToPlot {
print(x)
}
}
}
Any help would be much appreciated. Thanks
The error is because sineWave
tries to access the self
property sineArraySize
and others before self
was initialized (initialization happens after defining the values for properties). To work around this, you can do this:
var sineWave : [Double] = []
init() {
sineWave = (0..<sineArraySize).map {
amplitude1 * sin(2.0 * M_PI / Double(sineArraySize) * Double($0) * frequency1 + phase1)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With