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
}
}
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.
– Octavio Bertolucci