Swift ViewController在加载时崩溃

时间:2017-10-07 01:32:12

标签: swift view viewcontroller

我在iOS中制作了一个多转换工具来构建我的投资组合。但是,距离选项卡不会加载视图。它立即崩溃并给我两个错误。

当我尝试继续时,第二个出现。下面是错误和我的Swift类绑定到控制器以及应用程序的样子。

errors

import UIKit

class DistanceViewController: UIViewController, UITextFieldDelegate{
    @IBOutlet var userDistance: UITextField!
    @IBOutlet var resultLabel: UILabel!

    var fromKilometerValue: Measurement<UnitLength>?{
        didSet{
            milesConversion()
        }
    }

    var fromMileValue: Measurement<UnitLength>?{
        didSet{
            kilometerConversion()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        milesConversion()
        kilometerConversion()
    }

    //Dont forget to drag a gesture recognizer
    @IBAction func dismissKeyboard(_sender: UITapGestureRecognizer){
        userDistance.resignFirstResponder()
    }

    let numberFormatter: NumberFormatter = {
        let nf = NumberFormatter()
        nf.numberStyle = .decimal
        nf.minimumFractionDigits = 1
        nf.maximumFractionDigits = 1
        return nf
    }()

    func textField(_ userDistance: UITextField,
                   shouldChangeCharactersIn range: NSRange,
                   replacementString string: String) -> Bool {
        let existingTextHasDecimalSeparator = userDistance.text?.range(of: ".")
        let replacementTextHasDecimalSeparator = string.range(of: ".")

        if existingTextHasDecimalSeparator != nil,
            replacementTextHasDecimalSeparator != nil {
            return false
        } else {
            return true
        }
    }

    var toMileValue: Measurement<UnitLength>?{
        if let fromKilometerValue = fromKilometerValue{
            return fromKilometerValue.converted(to: .miles)
        }
        else{
            return nil
        }
    }

    var toKilometerValue: Measurement<UnitLength>?{
        if let fromMileValue = fromMileValue{
            return fromMileValue.converted(to: .kilometers)
        }
        else{
            return nil
        }
    }

    func milesConversion(){
        if let toMileValue = toMileValue {
            resultLabel.text = numberFormatter.string(from: NSNumber(value: Double(userDistance.text!)!))! + " km" + " is " + numberFormatter.string(from: NSNumber(value: toMileValue.value))! + " miles"
        }
    }

    func kilometerConversion(){
        if let toKilometerValue = toKilometerValue{
            resultLabel.text = numberFormatter.string(from: NSNumber(value: Double(userDistance.text!)!))! + " miles" + " is " + numberFormatter.string(from: NSNumber(value: toKilometerValue.value))! + " km"
        }
    }

    @IBAction func convertKilometers(_ sender: Any) {
        if let input = userDistance.text, let value = Double(input) {
            fromKilometerValue = Measurement(value: value, unit: .kilometers)
        } else {
            fromKilometerValue = nil
        }

        if(toMileValue == nil){
            resultLabel.text = "Unable to Convert " + userDistance.text!
        }
    }

    @IBAction func convertMiles(_ sender: Any) {
        if let input = userDistance.text, let value = Double(input) {
            fromMileValue = Measurement(value: value, unit: .miles)
        } else {
            fromMileValue = nil
        }

        if(toKilometerValue == nil){
            resultLabel.text = "Unable to Convert " + userDistance.text!
        }
    }
}

从我看到的内容中正确映射了类和视图。有人有任何想法吗?

编辑:我在Main.storyboard中没有旧连接,我删除它们并且视图加载得很好!

1 个答案:

答案 0 :(得分:0)

  

检查所有插座连接。并阅读stackoverflow.com/questions/32170456 / ... - rmaddy