Error clicking Button

0

Good morning!

I'm studying the Swift language for iOS and I have the following problem. I made a test application, available in the book "Swift - Programe for iPhone and iPad" of the Code House.

It is an application where I put a food and a note for it, and when you click add, it will appear on the screen in a list. The problem is that clicking the "Add" button displays the following error, in the class line, in the AppDelegate.swift file

  

Thread 1: SIGBRT signal

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

Follow the codes:

ViewController

import UIKit

class ViewController: UIViewController {

@IBOutlet var nameField: UITextField!
@IBOutlet var happinessField: UITextField!

@IBAction func add() {
    if nameField == nil || happinessField == nil {
        return
    }

    let name = nameField!.text
    let happiness = Int(happinessField!.text!)
    if happiness == nil {
        return }
    let meal = Meal(name: name!, happiness: happiness!)
    print("eaten: \(meal.name) \(meal.happiness)")

}
}

Meal:

import Foundation

class Meal {

let name: String
let happiness: Int
var items = Array<Item>()
init(name: String, happiness: Int) {
    self.name = name
    self.happiness = happiness
}

func allCalories() -> Double {
    print("Calculating")
    var total = 0.0
    for i in items {
        total += i.calories
    }
    return total
}

}

Item:

import Foundation

class Item {
let name: String
let calories: Double
init(name: String, calories: Double) {
    self.name = name
    self.calories = calories
}
}
    
asked by anonymous 02.08.2017 / 14:54

1 answer

0

If the app is initialized and only when the button is clicked the crash occurs, the problem is probably in the way that the button's click action was configured.

Open the storyboard file (or xib if applicable) and right click on the root (View Controller). Check if there is any problem connection, ie a yellow triangle with an exclamation mark. In your case the problem should be in the Received Actions section.

    
02.08.2017 / 16:19