Swift - Init致命错误

时间:2015-02-25 03:47:24

标签: ios function swift initialization

我正在尝试使用calculatecalculateBoundaryViewController2的函数初始化值。我遇到了运行时错误,如下图所示。

ViewController1

用户输入力,弹簧刚度和弹簧数的值,然后通过forceView2传递到stiffView2springNumView2prepareforsegue

2个公共职能

public func calculate (f:[Float], s:[Float], n:NSInteger) -> [Float]

public func calculateBoundary (inout f:[Float], s:[Float], n:NSInteger) -> (forceBound1:Float, forceBound2: Float, displacement:[Float])

ViewController2

import UIKit

class ViewController2: UIViewController , UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tableview2: UITableView!

    var forceView2 = [Float]()
    var stiffView2 = [Float]()
    var springNumView2 : NSInteger = NSInteger()

    var forceB1 : Float
    var forceB2 : Float
    var SpringD : [Float]

覆盖init(){

        if(isCheckedGlobal) {                          
            (forceB1,forceB2,SpringD) = calculateBoundary(&forceView2, stiffView2, springNumView2) }

        else {                        
        SpringD = calculate(forceView2, stiffView2, springNumView2)
        self.forceB1  = forceView2[0]
        self.forceB2 = 0.0 }

        super.init() 
    }

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

Run time error

1 个答案:

答案 0 :(得分:1)

缺少必需的init,将其放在您的插座下方:

required init(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
}

如果boolType == true是多余的,你绝不应该进行比较。

If isCheckedGlobal {...} 

如果您需要相反的比较,只需添加!在它面前:

If !isCheckedGlobal {...} 

if isCheckedGlobal {
     (forceB1,forceB2,springD) = calculateBoundary(&forceView2, stiffView2, springNumView2)
} else {
    springD = calculate(forceView2, stiffView2, springNumView2)
    forceB1  = forceView2[0]
    forceB2 = 0.0
}