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