带小数位的数学应用程序崩溃

Math app with decimal pad crashing

嗨,我正在尝试用方程式制作一个数学应用程序,我已经学会了如何获得十进制键盘,但是当我使用它时,它崩溃并显示有关线程一的消息:

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.

2016-02-16 21:13:31.682 Formel bok[586:43971] Can't find keyplane that supports type 8 for keyboard iPhone-PortraitChoco-DecimalPad; using 1022227301_PortraitChoco_iPhone-Simple-Pad_Default

2016-02-16 21:13:34.606 Formel bok[586:43971] Can't find keyplane that supports type 8 for keyboard iPhone-PortraitChoco-DecimalPad; using 1022227301_PortraitChoco_iPhone-Simple-Pad_Default

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) "

还有:

"Thread 1: EXC_BREAKPOIN(code=1,subcode=0x0002a476c)

我相信它能做到,因为它是 ,而不是 .点。我尝试将我的 phone 设置为美国地区然后我工作是否有其他解决方案来制作,使用此代码我真的很感谢收到的任何帮助。

import UIKit

class CylinderViewController: UIViewController {
    @IBOutlet weak var radien: UITextField!
    @IBOutlet weak var höjden: UITextField!
    @IBOutlet weak var svar: UILabel!
    @IBOutlet weak var svar2: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        radien.keyboardType = UIKeyboardType.DecimalPad
        höjden.keyboardType = UIKeyboardType.DecimalPad
       
        
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
        view.addGestureRecognizer(tap)          // Do any additional setup after loading the view.
    }

   
    
    func dismissKeyboard() {
        //Causes the view (or one of its embedded text fields) to resign the first responder status.
        view.endEditing(true)
        
        
        
        if höjden.text == ""{
        }
        else{
        
        let atext: Double? = Double(radien.text!)    // conversion of string to Int
        let btext: Double? = Double(höjden.text!)
       
        
        let myInt = atext! * atext!
       
        
        
        let result = Double(myInt * btext!) * M_PI
        svar.text = "\(result)"
        
        
        let pi = M_PI * 2
        
        let result2 = Float(pi) * Float(atext!) * Float(btext!)
        svar2.text = "\(result2)"
        
        
        
        
        }
        
    }
    
    
    
    
    
    
    
    
    

所以您遇到了几个错误。让我们从导致您崩溃的那个开始:

unexpectedly found nil while unwrapping an Optional value (lldb)

您收到此错误是因为您强制解包了一个 nil 值。 Swift 使用 if let 语句可以很好地防止这种情况发生。从字符串创建小数时尝试这样做:

if let double: Double? = Double(radien.text) {
    // Use double here
} else {
    // Error handling here
}

如果Swift能成功解析你的字符串,你就进入if语句,你就可以放心使用double变量了。如果 Swift 无法成功将您的字符串解析为 Double,这可能是因为您的字符串中有无效字符,您需要显示错误消息或其他内容

这是允许本地化的代码。我在美国和瑞典都试过,零问题消失了。

Note: You will need to do the number formatting on your output as well, but this should help you.

import UIKit

class CylinderViewController: UIViewController {
     @IBOutlet weak var radien: UITextField!
     @IBOutlet weak var höjden: UITextField!
     @IBOutlet weak var svar: UILabel!
     @IBOutlet weak var svar2: UILabel!

  override func viewDidLoad() {
      super.viewDidLoad()
      radien.keyboardType = UIKeyboardType.DecimalPad
      höjden.keyboardType = UIKeyboardType.DecimalPad


    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
    view.addGestureRecognizer(tap)          // Do any additional setup after loading the view.
  }

   func dismissKeyboard() {
       //Causes the view (or one of its embedded text fields) to resign the first responder status.
     view.endEditing(true)

    if höjden.text == ""{
    }
    else{

        let nf = NSNumberFormatter() // added to use localization
        let atext = nf.numberFromString(radien.text!) //added
        let btext = nf.numberFromString(höjden.text!) //added
       //let atext: Double? = Double(radien.text!)    // conversion of string to Int
       //let btext: Double? = Double(höjden.text!)

        let myInt = Double(atext!) * Double(atext!)

        let result = Double(myInt * Double(btext!)) * M_PI
        svar.text = "\(result)"

        let pi = M_PI * 2

        let result2 = Float(pi) * Float(atext!) * Float(btext!)
        svar2.text = "\(result2)"

    }

 }

}