1

I am trying to write a simple guessing swift app for iPhone. The code runs successfully, displaying "Build Succeeded". However, I keep getting this message for a particular line: "Thread 1: signal SIGABRT".

The line contains "num += Int( (rand()%4) + 1)", which works fine in a playground.

Can someone tell me how to fix this problem?

import UIKit

class ViewController: UIViewController {


var num = 0


@IBOutlet var GuessField: UITextField!


@IBOutlet var ResultLabel: UILabel!


@IBOutlet var ScoreLabel: UILabel!


@IBAction func NewGameButton(sender: UIBarButtonItem) {


num += Int( (rand()%4) + 1)

    /* Random numbers generated at num range from 1 to 4, which respectively
    correspond to strings BMW, Mercedes, Lamborgini, and Ford. */

}




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 ResultButton(sender: AnyObject) {

    if GuessField.text == "BMW" {
        if num == 1 {ResultLabel.text = "You Win!"}
        else {ResultLabel.text = "Try Again!"}

    }

    if GuessField.text == "Mercedes" {
        if num == 2 {ResultLabel.text = "You Win!"}
        else {ResultLabel.text = "Try Again!"}

    }


    if GuessField.text == "Lamborgini" {
        if num == 3 {ResultLabel.text = "You Win!"}
        else {ResultLabel.text = "Try Again!"}

    }

    if GuessField.text == "Ford" {
        if num == 4 {ResultLabel.text = "You Win!"}
        else {ResultLabel.text = "Try Again!"}

    }


}

}
mhyousefi
  • 1,064
  • 2
  • 16
  • 30
  • After adding outlets, did you remove any control from the UI? – neo Jul 23 '15 at 18:55
  • The only change I made was this: for the "NewGameButton" (which btw is where the error is occurring) I created a "Button" first, but then removed it and made it a "Bar Button Item". – mhyousefi Jul 24 '15 at 06:03
  • Did you create an outlet connection for the button you removed? If you did, then deleting the button doesn't delete the outlet connection. Does the error you get say anything about your class not implementing some protocol? – neo Jul 24 '15 at 11:26
  • I think the issue was what you pointed out. I created a new project and did it all over again, and I was not getting the error this time. It definitely had something to do with the outlet connection. Thank you. – mhyousefi Jul 24 '15 at 14:33
  • So I am going to add my comment as an answer so people can know that the question is answered. – neo Jul 24 '15 at 15:07

2 Answers2

2

If you created a Control and added an outlet connection to the view controller and then removed the Control from the page before removing the outlet connection properly from the Control's connections, then "Thread 1: signal SIGABRT" error happens. I had the same issue before. It might also be saying that your class is not KeyValueCoding compliant or something. Make sure you clear all connections of the control before you remove the control.

neo
  • 1,952
  • 2
  • 19
  • 39
1

Try saying num = Int( (rand()%4) + 1) instead of num += Int( (rand()%4) + 1)

ab341
  • 13
  • 4