xcode Swift 2.0 映射“实例成员不能用于类型”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32540878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:48:27  来源:igfitidea点击:

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

xcodeswiftdictionaryfft

提问by Sole

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.

我正在尝试学习如何使用 swift 2.0 创建 FFT,但是我无法编译 .map 函数。

The following code works in a playground but not inside xCode as a member of a swift class.

以下代码在操场上工作,但在 xCode 中作为 swift 类的成员不工作。

I get the following error "Instance member 'sineArraySize' cannot be used on type 'FFTAnalyser'

我收到以下错误“实例成员 'sineArraySize' 不能用于类型 '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(
var sineWave : [Double] = []

init() {
    sineWave = (0..<sineArraySize).map {
        amplitude1 * sin(2.0 * M_PI / Double(sineArraySize) * Double(##代码##) * frequency1 + phase1)
    }
}
) * frequency1 + phase1) } func plotArray<T>(arrayToPlot:Array<T>) { for x in arrayToPlot { print(x) } } }

Any help would be much appreciated. Thanks

任何帮助将非常感激。谢谢

采纳答案by Kametrixom

The error is because sineWavetries to access the selfproperty sineArraySizeand others before selfwas initialized (initialization happens after defining the values for properties). To work around this, you can do this:

错误是因为在初始化之前sineWave尝试访问self属性sineArraySize和其他属性self(初始化发生在定义属性值之后)。要解决此问题,您可以执行以下操作:

##代码##