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
Swift 2.0 Map "Instance member cannot be used on type"
提问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 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:
错误是因为在初始化之前sineWave
尝试访问self
属性sineArraySize
和其他属性self
(初始化发生在定义属性值之后)。要解决此问题,您可以执行以下操作: