Swift ViewController 在加载时崩溃
Posted
技术标签:
【中文标题】Swift ViewController 在加载时崩溃【英文标题】:Swift ViewController Crashes on Load 【发布时间】:2017-10-07 01:32:12 【问题描述】:我正在 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 中不存在,我删除了它们并且视图加载正常!
【问题讨论】:
检查所有插座连接。并阅读***.com/questions/32170456/… @rmaddy 成功了!谢谢! 【参考方案1】:检查所有插座连接。并阅读 ***.com/questions/32170456/... – rmaddy
【讨论】:
以上是关于Swift ViewController 在加载时崩溃的主要内容,如果未能解决你的问题,请参考以下文章
当我尝试重新加载日期时,Swift 5 TableView 在 ViewController 中发现 nil
在 swift 3 中启动应用程序时,如何在另一个 ViewController 中预加载 UIWebView?