Error while clicking Button

Asked

Viewed 40 times

0

Good morning!

I am 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" Code House.

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

Thread 1: SIGBRT Signal

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

Follows 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
}
}

1 answer

0


If the app is initialized and only when clicking on the button happens the crash, probably the problem is in the way the button click action was configured.

Open the storyboard file (or xib if applicable) and right-click the root (View Controller). Check if there is any connection with problem, that is, a yellow triangle with an exclamation. In your case the problem must be in the section Received Actions. inserir a descrição da imagem aqui

  • Thanks friend. That’s right.After some modifications in the organization of the files, you must have lost the link to the code with the button.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.