Reputation: 1234
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!"}
}
}
}
Upvotes: 1
Views: 1479
Reputation: 2042
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.
Upvotes: 2
Reputation: 13
Try saying num = Int( (rand()%4) + 1)
instead of num += Int( (rand()%4) + 1)
Upvotes: 1