Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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?

share|improve this question

When you do infinitive = MainViewController().input, MainViewController() just creates a new instance of MainViewController. That new instance never calls textFieldShouldReturn and therefore its input will be nil. Instead, add a reference from Conjugate to MainViewController.

var mainViewController: MainViewController?

Then in textFieldShouldReturn create a Conjugate and assign its property:

let conjugate = Conjugate()
conjugate.mainVieController = self

Then in the conjugate() method, instead of creating a new view controller, refer to the property:

func conjugate() {

  // gets verb from text field
  infinitive = self.ainViewController.input

  // checks verb ending and sets value to booleans
  if (infinitive.hasSuffix("ar")) {
      isEndingAr = true
      }
  }
}

Also, like @emresancaktar said, input should be optional, since it might be nil.

share|improve this answer

You initialize a new MainViewController instance in conjugate() method which is totally a different object than the actual main view controller.

You should figure some way out for passing the textField's text onto Conjugate class:

One way would be:

class Conjugate {
  ...

  conjugateWithController(viewController: MainViewController) {
    if let infinitive = viewController.input {
      ...
    }
  }       
}

and you should pass the actual MainViewController instance to that method instead of creating a local one.

share|improve this answer
var infinitive: String!
var input: String!

Thats the problem. You did not initiate your class so its nil. You should use the ? for the variable.

try these :

var infinitive: String?
var input: String?
share|improve this answer
    
That's not the main problem here. The problem is, he doesn't understand objective oriented programming yet. Removing implicitly unwrapping won't do much. – Eendje 18 mins ago
    
Yea I know. But what can we do ? Should we downvote him ? I tried to save him implicitly unwrap error. I thought he will see variables are nil and he will wants to know what is the problem so he will learn something. This is my think. You should not downvote me. Whatever. – emresancaktar 15 mins ago
    
I didn't down vote you. I think you've been down voted because your answer doesn't answer his question in any way. I want to point him out for some good documentation, but I haven't really read them myself so I can't find them as fast :p – Eendje 13 mins ago
    
@emresancaktar I downvoted your answer because it has nothing to do with the question OP asked. However, you might want to leave a comment to his question instead of writing an answer since you definitely refer good points. – ozgur 8 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.