数学应用程序与小数点崩溃

时间:2016-02-16 18:03:56

标签: xcode swift math keyboard decimal

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

  

在UIViewAlertForUnsatisfiableConstraints处创建一个符号断点,以便在调试器中捕获它。   UIView中列出的UIVonstraintBasedLayoutDebugging类别中的方法也可能有所帮助。

     

2016-02-16 21:13:31.682 Formel bok [586:43971]无法找到支持键盘iPhone-PortraitChoco-DecimalPad的8型键盘;使用1022227301_PortraitChoco_iPhone-Simple-Pad_Default

     

2016-02-16 21:13:34.606 Formel bok [586:43971]无法找到支持键盘iPhone-PortraitChoco-DecimalPad的8型键盘;使用1022227301_PortraitChoco_iPhone-Simple-Pad_Default

     

致命错误:在展开Optional值时意外发现nil   (lldb)“

但是:

  

“主题1:EXC_BREAKPOIN(代码= 1,子代码= 0x0002a476c)

我相信它是因为它是a而不是a。点。我尝试将手机设置为美国地区然后我工作是否有一些其他解决方案,使用此代码我真的很感谢任何帮助。

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)"
        
        
        
        
        }
        
    }
    
    
    
    
    
    
    
    
    

2 个答案:

答案 0 :(得分:0)

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

  

在展开Optional值(lldb)时意外发现nil

您收到此错误是因为您强制解包零值。 Swift有一种使用if let语句来防止这种情况的好方法。从字符串创建小数时尝试执行此操作:

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

如果Swift可以成功解析您的字符串,您将输入if语句,并且可以安全地使用double变量。如果Swift无法将您的字符串成功解析为Double,则可能是因为字符串中包含无效字符,您需要显示错误消息或其他内容

答案 1 :(得分:0)

这是您的代码,可以进行本地化。我和美国和瑞典都试过了,没有问题就没了。

  

注意:您还需要在输出中进行数字格式化,但这对您有所帮助。

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)"

    }

 }

}