我是建立计算器的最初方法。目前,当用户点击数字时,代码只会将数字和Pi打印到计算器的标签中。
1)Che Code
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var display: UILabel! = nil
var userIsInTheMiddleOfTypeing = false
@IBAction func touchDigit(_ sender: UIButton){
let digit = sender.currentTitle!
if userIsInTheMiddleOfTypeing {
let textCurrentlyInDisplay = display.text!
display.text = textCurrentlyInDisplay + digit
} else {
display.text = digit
}
userIsInTheMiddleOfTypeing = true
}
@IBAction func performOperation(_ sender: UIButton) {
userIsInTheMiddleOfTypeing = false
if let methematicalSymbol = sender.currentTitle {
if methematicalSymbol == "π" {
display.text = String(M_PI) // M_PI
}
}
}
}
2)用户界面
touchDigit 功能链接到所有数字按钮,如下图所示。 显示是UILable
,而 performOperaton 是PI按钮
问题
当我构建代码时,Xcode首先告诉我构建成功,但是,在我做任何事情之前,会出现一个错误,如下所示
错误日志(从调试区域复制)
2016-07-28 19:30:30.215343 Calculator[11671:208157] bundleid: com.Jeffery.Calculator, enable_level: 0, persist_level: 0, propagate_with_activity: 0
2016-07-28 19:30:30.218796 Calculator[11671:208157] Created DB, header sequence number = 260
2016-07-28 19:30:30.767300 Calculator[11671:208178] subsystem: com.apple.UIKit, category: HIDEvents, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0
2016-07-28 19:30:31.022078 Calculator[11671:208157] Created DB, header sequence number = 260
2016-07-28 19:30:31.350380 Calculator[11671:208157] subsystem: com.apple.BaseBoard, category: MachPort, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0
2016-07-28 19:30:31.388363 Calculator[11671:208159] subsystem: com.apple.FrontBoard, category: Common, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
答案 0 :(得分:0)
我认为问题在这里:' @IBOutlet弱变显示:UILabel! = nil'。当您使用'显示'做某事时您尝试用nil做的财产。只需尝试删除' = nil'声明财产后。或者你可以覆盖视图控制器初始化方法,但就我而言,在这种情况下它是不好的方法。
答案 1 :(得分:0)
我想出了我的错误,PI按钮附加了两个连接到viewControler(我应该删除一个)。