Swift线程1:EXC_BREAKPOINT错误

时间:2016-11-28 20:54:04

标签: ios swift xcode

我是一个快速的新手,而现在这是a $$代码的痛苦。 我试图创建一个非常简单的应用程序,用户在文本字段中输入数字 然后当单击一个按钮时,它会在标签中显示数字+ 1。 我总是得到这个错误:

  

主题1:EXC_BREAKPOINT(代码= 1,子代码= 0x1002c11ec)

这是我的代码:

    import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var tav: UITextField!
    @IBOutlet weak var fogy: UITextField!
    @IBOutlet weak var ar: UITextField!
    @IBOutlet weak var eredmeny: UILabel!
    @IBOutlet weak var szamolasBtn: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func szamol(_ sender: Any) {
        let number = Int(tav.text!)! + 1
        eredmeny.text = String(describing: number)
    }

    }

1 个答案:

答案 0 :(得分:0)

我运行了你的代码,如果在文本字段中输入一个整数,它可以正常工作。但是如果您输入除字符串以外的任何字符,那么应用程序崩溃,因为 Int(tav.text!)!返回 nil 并将1添加到 nil 。首先检查 nil ,然后添加内容。

相关问题