无法在属性初始值设定项中使用实例成员

时间:2017-08-21 07:11:39

标签: ios swift declaration

我已经检查了other case on this,但懒惰的var似乎无法解决我的问题。

我有这个代码用于解决某个等式,一旦解决了,我想将我得到的2个数组作为答案返回到我ViewController.swift中的原始代码。代码如下所示,但在我尝试用let (dollars1, dollars2) = prob.solveUsingSolver(solver:solveEuler)行实际解决问题的那一行。我最初在操场上用prob.solveUsingSolver(solver:solveEuler)测试它,这解决了问题。但是,在主应用程序中,它给出了“预期声明”错误。如果我尝试将输出分配给dollars1dollars2,我会得到

  

不能在属性初始值设定项中使用实例成员'prob';属性初始化程序在'self'可用之前运行

import Charts
class ViewController: UIViewController, ChartViewDelegate {
    @IBOutlet weak var lineChartView: LineChartView!
    override func viewDidLoad()
    {
        super.viewDidLoad()
        // Solving the problem
        let wholearray = HE(Th1: 100.0, Tc2: 25.0, deltaZ: 0.1).solveUsingSolver(solver:solveEuler)
        let temp_hot = wholearray.xarray
        let temp_cold = wholearray.varray
        // 1
        self.lineChartView.delegate = self
        // 2
        self.lineChartView.chartDescription?.text = "Tap node for details"
        // 3
        self.lineChartView.descriptionTextColor = UIColor.white
        self.lineChartView.gridBackgroundColor = UIColor.darkGray
        // 4
        self.lineChartView.noDataText = "No data provided"
        // 5
        setChartData(space_dimensionless: space_dimensionless)
    }
    let space_dimensionless = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
    typealias Solver = (Double, Double, Double) -> (Array<Double>, Array<Double>)
    struct HE {
        var Th1 = 0.0
        var Tc2 = 0.0
        var deltaZ = 0.0
        init(Th1: Double, Tc2: Double, deltaZ: Double) {
            self.Th1 = Th1
            self.Tc2 = Tc2
            self.deltaZ = deltaZ
        }
        func solveUsingSolver(solver: Solver) {
            solver(Th1, Tc2, deltaZ)
        }
    }
    func solveEuler(Th1: Double, Tc2: Double, z : Double) -> (xarray:Array<Double>, varray:Array<Double>)
    {
        var x = Th1
        var i = 0.0
        var upper = Th1
        var lower = Tc2
        var vold = (lower + upper) / 2
        var v = vold
        var xold = x
        let epsilon = 0.05
        var err = 1.0
        var xarray: [Double] = [0,1,2,3,4,5,6,7,8,9,10]
        var varray: [Double] = [0,1,2,3,4,5,6,7,8,9,10]
        var j=0
        while (err > epsilon) {
            while i < 1 {
                v -= 4*z*i*(xold-vold)
                x -= z*i*(xold-vold)
                xold = x
                vold = v
                i+=z
                xarray[j]=x
                varray[j]=v
                j += 1
            }
            err = abs(varray[10] - Tc2)
            if varray[10] > Tc2 {
                upper = varray[0]
            }
            else {
                lower = varray[0]
            }
            x = Th1
            xold = x
            vold = (lower + upper) / 2
            v = vold
            i=0.0
            j=0
        }
        return (xarray,varray)
    }
    func setChartData(space_dimensionless : [Double]) {
        // 1 - creating an array of data entries
        var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
        for i in 0 ..< space_dimensionless.count {
            yVals1.append(ChartDataEntry(x: space_dimensionless[i], y: temp_hot[i]))
        }
        // 2 - create a data set with our array
        let set1: LineChartDataSet = LineChartDataSet(values: yVals1, label: "Hot Fluid")
        set1.axisDependency = .left // Line will correlate with left axis values
        set1.setColor(UIColor.red.withAlphaComponent(0.5)) // our line's opacity is 50%
        set1.setCircleColor(UIColor.red) // our circle will be dark red
        set1.lineWidth = 2.0
        set1.circleRadius = 0.0 // the radius of the node circle
        set1.fillAlpha = 65 / 255.0
        set1.fillColor = UIColor.red
        set1.highlightColor = UIColor.white
        set1.drawCircleHoleEnabled = false
        // 3 - creating an array of data entries
        var yVals2 : [ChartDataEntry] = [ChartDataEntry]()
        for i in 0 ..< space_dimensionless.count {
            yVals2.append(ChartDataEntry(x: space_dimensionless[i], y: temp_cold[i]))
        }
        // 4 - create a data set with our array
        let set2: LineChartDataSet = LineChartDataSet(values: yVals2, label: "Cold Fluid")
        set2.axisDependency = .left // Line will correlate with left axis values
        set2.setColor(UIColor.blue.withAlphaComponent(0.5)) // our line's opacity is 50%
        set2.setCircleColor(UIColor.blue) // our circle will be dark red
        set2.lineWidth = 2.0
        set2.circleRadius = 0.0 // the radius of the node circle
        set2.fillAlpha = 65 / 255.0
        set2.fillColor = UIColor.blue
        set2.highlightColor = UIColor.white
        set2.drawCircleHoleEnabled = true
        //5 - create an array to store our LineChartDataSets
        var dataSets = [IChartDataSet]()
        dataSets.append(set1)
        dataSets.append(set2)
        let lineChartData = LineChartData(dataSets: dataSets)
        //6 - set our data
        lineChartView.data = lineChartData
        //7 Format chart more
        self.lineChartView.xAxis.drawGridLinesEnabled = false
        self.lineChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
        self.lineChartView.xAxis.labelFont = UIFont(name: "HelveticaNeue-Light", size: 10.0)!
        self.lineChartView.xAxis.labelTextColor = UIColor.black
        self.lineChartView.xAxis.drawAxisLineEnabled=true
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

1 个答案:

答案 0 :(得分:1)

您要声明两个属性,prob(dollars1, dollars2)元组

let prob = HE(Th1: 100.0, Tc2: 25.0, deltaZ: 0.1)
let (dollars1, dollars2) = prob.solveUsingSolver(solver:solveEuler)

但此时您无法使用probsolveUsingSolver函数)的成员,因为self.prob尚未初始化。

您可能希望在某个函数中执行元组赋值。

<强>更新

您的solveUsingSolver函数会调用solver,但不会从解算器返回值。它应该是:

func solveUsingSolver(solver: Solver) -> (Array<Double>, Array<Double>) {
    return solver(Th1, Tc2, deltaZ)
}