Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2.0 Map "Instance member cannot be used on type"

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

like image 578
Sole Avatar asked Sep 12 '15 16:09

Sole


Video Answer


1 Answers

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)
    }
}
like image 154
Kametrixom Avatar answered Sep 24 '22 17:09

Kametrixom