我已经检查了other case on this但是lazy 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.
    }
}

最佳答案

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

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

但此时不能使用prob的成员(函数),因为solveUsingSolver尚未初始化。
您可能希望元组赋值在某个函数中执行。
更新
self.prob函数调用solveUsingSolver但不从解算器返回值。应该是:
func solveUsingSolver(solver: Solver) -> (Array<Double>, Array<Double>) {
    return solver(Th1, Tc2, deltaZ)
}

关于ios - 无法在属性初始化程序中使用实例成员,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45791206/

10-09 08:04