当HourGoal和MinuteGoal文本字段为空时,我无法识别代码。它们永远不会出现nil值或为空,导致程序在我尝试打开值时崩溃。
import UIKit
class ReadingGoal: UIViewController {
@IBOutlet weak var HourGoal: UITextField!
@IBOutlet weak var MinuteGoal: UITextField!
@IBOutlet weak var NextButton: UIButton!
var mingoal = Int()
var hrgoal = Int()
var secondstoread:Int = 0
var initialtime:Int = Int()
@IBAction func HourEditingDidChange(_ sender: Any) {
NextButton.isEnabled = true
}
@IBAction func MinuteEditingDidChange(_ sender: Any) {
NextButton.isEnabled = true
}
func disable() {
NextButton.isEnabled = false
}
@IBAction func NextButtonIsPressed(_ sender: Any) {
print("GERALDINE IS AWESOME!!!!!")
// ERROR
if HourGoal.text?.isEmpty == false || MinuteGoal.text?.isEmpty == false {
if HourGoal.text != nil && HourGoal.text?.isEmpty == false{
// self.hrgoal = Int(HourGoal.text!)!
print("hr Value exists")
print("\(HourGoal.text!)")
}
else {
NextButton.isEnabled = false
}
if MinuteGoal.text != nil && MinuteGoal.text?.isEmpty == false{
// self.mingoal = Int(MinuteGoal.text!)!
print("min value exists")
print("\(MinuteGoal.text!)")
}
else {
NextButton.isEnabled = false
}
print("in ")
// secondstoread = ((mingoal + (hrgoal*60))*60)
secondstoread = 10
if secondstoread > 0 {
performSegue(withIdentifier: "Mr.Friar-DavisIsAGiantBallOfFloof", sender: self)
}
else {
NextButton.isEnabled = false
}
//function saves text field info to core data
}
}
//for the specific segue 'toBookInfo', certain information is passed through the segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//making sure we are using correct segue
if segue.identifier == "Mr.Friar-DavisIsAGiantBallOfFloof" {
//selecting the destination controller
let detailVC: TimerController = segue.destination as! TimerController
//sending properties to destination view controller
detailVC.seconds = secondstoread
}
else {
// Error sender is not a cell or cell is not in collectionView.
}
}
override func viewDidLoad() {
super.viewDidLoad()
initialtime = secondstoread
}
}
答案 0 :(得分:0)
这里不会教你术语,但所有变量都应该以 lowerCase 开头,就像这样,甚至IBOutlets ......
......好吧,我研究了你的代码一段时间了:
以任何成本避免使用激励标记!!! - >它因为你使用而崩溃的原因!无处不在,那种力量打破了选择权,完全破坏了迅捷的美丽。
这条线如何适用于编译器对选项没有问题?
if HourGoal.text?.isEmpty == false || MinuteGoal.text?.isEmpty == false
你以某种方式检查可选值,并且值肯定为零,所以在代码行提供之前:
guard let hourGoalText = HourGoal.text, let minuteGoal = minuteGoal.text else { //handle code for empty textField
return }
这是检查选项的第一步......现在代码不会崩溃......但老实说没有调试
如果你使用guard let或者let语句,你就不会遇到崩溃问题
答案 1 :(得分:0)
您可以通过不同方式检查textfield
空虚。其中一些是:
if textField.text?.isEmpty ?? true{
print("textField is empty")
}
else{
print("textField has some text")
}
或
if textfield.text?.characters.count==0 {
print("textField is empty")
}
else{
print("textField has some text")
}
或
if textfield.text == "" {
print("textField is empty")
}
else{
print("textField has some text")
}
答案 2 :(得分:0)
使用if let
或guard
以安全的方式展开。
像这样的东西
if let texthour = HourGoal.text{
// has text and can be "", next check for blank string
if texthour == ""{
}
}
或使用guard
作为Dominik显示