I am trying to make a simple application in Xcode that will calculate digits of Pi to a specific digit (i.e. the 100th digit) and show the time it took to do this. The first step that I am running into is that I want to have a TextField that only accepts an integer value, and when that condition is met, then the button to "start" the program will then be enabled to be pressed. For all other conditions, I would like an error to be thrown saying "Must enter an integer value to continue." but I am having difficulty finding out why this is the case.
I have followed the advice from this thread but I keep getting the error: "Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value" with all of the options that are given.
The only thing I got to compile was the suggestion made by @Naishta all others gave me that error I mentioned before. Here is my code that I have that is able to compile:
@IBOutlet weak var FirstBox: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
//FirstBox.delegate = self
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let _ = Int(string) else {
button.isEnabled = false
enum MyError: Error {
case runtimeError(String)
}
return false
}
button.isEnabled = true
return true
}
and the transition that I am using to go to the next page is just the standard:
@IBAction func StartCalc(_ sender: Any) {
self.performSegue(withIdentifier: "CalcSegue", sender: self)
}
I am expecting to see that when anything besides an integer is written that the button is not able to go to the next page, and for it to throw an error.