In ViewController.swift
, I have a text field whose input I would like to use and manipulate in my other class, Conjugate.swift
. I am capturing the input at the same time the keyboard is hidden, like so:
import UIKit
class MainViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var mainTextField: UITextField!
var input: String!
override func viewDidLoad() {
super.viewDidLoad()
mainTextField.delegate = self
}
/* KEYBOARD HIDE */
func textFieldShouldReturn(textField: UITextField) -> Bool {
input = self.mainTextField.text
textField.resignFirstResponder()
return true;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
And then in my other class:
class Conjugate {
var infinitive: String!
var isEndingAr = false
func conjugate() {
// gets verb from text field
infinitive = MainViewController().input
// checks verb ending and sets value to booleans
if (infinitive.hasSuffix("ar")) {
isEndingAr = true
}
}
}
Here is the error I'm getting:
fatal error: unexpectedly found nil while unwrapping an Optional value
I know that input
in MainViewController.swift
is still nil. I just don't know why. What am I doing wrong?