Cherry_thia
Cherry_thia

Reputation: 696

Swift - Init fatal error

I am trying initialise values by using functions of calculate and calculateBoundary in ViewController2. I got an run time error, shown in the image below.

ViewController1

User inputs values of forces, spring stiffnesses and spring number which are then passed into forceView2, stiffView2 and springNumView2, by prepareforsegue.

2 public Functions

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]

override 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

Upvotes: 1

Views: 498

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236360

The required init is missing, put it below your outlets:

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

You should never do a comparison if boolType == true is redundant.

If isCheckedGlobal {...} 

If you need the opposite comparison just add ! In front of it:

If !isCheckedGlobal {...} 

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

Upvotes: 1

Related Questions