简体   繁体   中英

Problems getting around 'Super.init isn't called on all paths before returning from initializer swift.'

I'm trying to implement a BPM counter into a basic iOS app.

I'm using this implementation in an XCode project, connecting the addTap method to an onscreen button.

However, I receive the error.

Super.init isn't called on all paths before returning from initializer swift.

So following this answer I found by searching here, I realised I have to call super.init() as defined in the UIViewController class.

I've done that, as shown below - but now I'm getting another compiler error

Cannot convert value of type 'NSCoder.Type' to expected argument type 'NSCoder'

What I have at the minute is pasted below, TIA

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var display: UILabel!

private let timeOutInterval: TimeInterval
private let minTaps: Int
private var taps: [Date] = []

init(timeOut: TimeInterval, minimumTaps: Int) {
    timeOutInterval = timeOut
    minTaps = minimumTaps
    super.init(coder:NSCoder)
}

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



func addTap() -> Double? {
    let thisTap = NSDate()

    if let lastTap = taps.last {
        if thisTap.timeIntervalSince(lastTap) > timeOutInterval {
            taps.removeAll()
        }
    }

    taps.append(thisTap as Date)
    guard taps.count >= minTaps else { return nil }
    guard let firstTap = taps.first else { return nil }

    let avgIntervals = thisTap.timeIntervalSince(firstTap) / Double(taps.count - 1)
    return 60.0 / avgIntervals
}


@IBAction func button(_ sender: Any) {
self.display.text = String(addTap())
}

}

Your issue is with the line:

super.init(coder:NSCoder)

The parameter needs to be an instance of NSCoder , not the class itself. But you shouldn't even be calling that initializer.

Change that line to:

super.init(nibName: nil, bundle: nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM