1

So I'm trying to program an "Mad Lib" themed app for a class I'm in but I keep getting a

EXC_BAD_INSTRUCTION

error when I hit the submit button. The other students in the class couldn't figure it out either. Please help me!

Here is my firstViewController.swift:

import UIKit

class ViewController: UIViewController
{
    @IBOutlet weak var firstTextField: UITextField! //adjective
    @IBOutlet weak var secondTextField: UITextField! //male name
    @IBOutlet weak var thirdTextField: UITextField! //verb
    @IBOutlet weak var fourthTextField: UITextField! //female name
    @IBOutlet weak var fifthTextField: UITextField! //adjective
    @IBOutlet weak var sixthTextField: UITextField! //athlete
    @IBOutlet weak var seventhTextField: UITextField! //food
    @IBOutlet weak var eighthTextField: UITextField! //restaurant name
    var userInfo = myCustomClass()

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        userInfo.adjectiveOne = firstTextField.text!
        userInfo.maleName = secondTextField.text!
        userInfo.verb = thirdTextField.text!
        userInfo.femaleName = fourthTextField.text!
        userInfo.adjectiveTwo = fifthTextField.text!
        userInfo.athlete = sixthTextField.text!
        userInfo.food = seventhTextField.text!
        userInfo.restaurantName = eighthTextField.text!
        let nextView = segue.destination as! secondViewController
        nextView.passedObject = userInfo
    }
}

Here is my secondViewController.swift:

import UIKit

class secondViewController: UIViewController
{
    var passedObject = myCustomClass()
    @IBOutlet weak var myFinishedProduct: UILabel!

    override func viewDidLoad()
    {
        super.viewDidLoad()
         myFinishedProduct.text = "There was once a \(passedObject.adjectiveOne) man /n \(passedObject.maleName). One day while he was \(passedObject.verb) he saw /n \(passedObject.femaleName), a rather \(passedObject.adjectiveTwo) woman. /n She was also a \(passedObject.athlete), and a very good /n one too. The two went to lunch together at \(passedObject.restaurantName) /n and ate some \(passedObject.food). After /n that they never crossed paths again."
    }
}

Finally here is my NSOBject called "myCustomClass.swift":

import UIKit

class myCustomClass: NSObject
{
    var adjectiveOne = ""
    var maleName = ""
    var verb = ""
    var femaleName = ""
    var adjectiveTwo = ""
    var athlete = ""
    var food = ""
    var restaurantName = ""
}

Basically, `whatever the user enters into the eight text fields will be stored in myCustomClass when the submit button is pressed. From there, in the secondViewController it will put the eight inputs into the story and display it on a label.

Any help is appreciated, thank you!

Edit: The "Submit Button" is connected to the secondViewController on my storybook with the purpose of "show".

ɐlǝx
  • 1,384
  • 2
  • 17
  • 22
  • Add an exception breakpoint in Xcode and let us know where the issue happens and what exact error is shown. Probably one of the multible `!` in `prepare`. – shallowThought Jun 13 '17 at 13:53
  • Also add the full error message to your question and use the debugger to step through your code. It should take no more than 1 minute to find the error (and if you read the full error message and add an exception breakpoint it should be immediate). – Robotic Cat Jun 13 '17 at 14:07

1 Answers1

0

First View Controller

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var txtmobile: UITextField!
    @IBOutlet weak var txtlname: UITextField!
    @IBOutlet weak var txtfname: UITextField!
    var ArrayStudent:[PassData] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func btnclick(_ sender: UIButton)
    {
        let objdata = PassData(fname: txtfname.text!, lname: txtlname.text!, mobile: txtmobile.text!)
        ArrayStudent.append(objdata)
        passdata()

    }
    func passdata()
    {
        let objstory = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
        objstory.Arradata1 = ArrayStudent
        _ = self.navigationController?.pushViewController(objstory, animated: true)
    }

}

Second View Controller

  import UIKit

    class SecondViewController: UIViewController {

        @IBOutlet weak var lblmobile: UILabel!
        @IBOutlet weak var lbllastname: UILabel!
        @IBOutlet weak var lblname: UILabel!
        var Arradata1:[PassData ] = []
        override func viewDidLoad() {
            super.viewDidLoad()
            lblname.text = Arradata1.first?.StrFirstName
            lbllastname.text = Arradata1.first?.StrLastName
            lblmobile.text = Arradata1.first?.StrMobile

            // Do any additional setup after loading the view.
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

NSObject Class

import UIKit

    class PassData: NSObject
    {
        var StrFirstName:String!
        var StrLastName:String!
        var StrMobile:String!
        init(fname:String , lname:String , mobile:String)

        {
            StrMobile = mobile
            StrFirstName = fname
            StrLastName = lname
        }
    }
Amul4608
  • 1,390
  • 14
  • 30