-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathContents.swift
45 lines (36 loc) · 897 Bytes
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import UIKit
// App Exercise - Guard
struct Workout {
var startTime: Double
var endTime: Double
init?(startTime: Double, endTime: Double) {
guard (endTime - startTime) < 10 else {
return nil
}
self.startTime = startTime
self.endTime = endTime
}
}
// ------
struct Food {
var name: String
var calories: Int
}
let foodTextField = UITextField()
let caloriesTextField = UITextField()
foodTextField.text = "Banana"
caloriesTextField.text = "23"
func logFood() -> (Food?) {
guard let food = foodTextField.text, let calories = caloriesTextField.text else {
return nil
}
guard let realCalories = Int(calories) else {
return nil
}
return Food(name: food, calories: realCalories)
}
var foodLogged = logFood()
if let result = foodLogged {
print(result.name)
print(result.calories)
}