-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathContents.swift
55 lines (41 loc) · 1.18 KB
/
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
46
47
48
49
50
51
52
53
54
55
import UIKit
// Exercise - Guard Statements
func calculateArea(x: Double, y: Double) -> (Double?) {
guard x < 0 || y < 0 else {
return nil
}
return x * y
}
print(calculateArea(x: 4.0, y: 5.5))
print(calculateArea(x: -2.4, y: 7.0))
// -------
func add(a: Int?, b: Int?) -> (Int?) {
guard let firstNumber = a, let secondNumber = b else {
return nil
}
return firstNumber + secondNumber
}
print(add(a: 6, b: 4))
print(add(a: nil, b: 7))
// ------
struct User {
var firstName: String
var lastName: String
var age: String
}
let firstNameTextField = UITextField()
let lastNameTextField = UITextField()
let ageTextField = UITextField()
firstNameTextField.text = "Jonathan"
lastNameTextField.text = "Sanders"
ageTextField.text = "28"
func createUser() -> (User?){
guard let firstName = firstNameTextField.text, let lastName = lastNameTextField.text, let age = ageTextField.text else {
return nil
}
return User(firstName: firstName, lastName: lastName, age: age)
}
let newUser = createUser()
if let firstName = newUser?.firstName, let lastName = newUser?.lastName, let age = newUser?.age {
print("\(firstName) \(lastName) \(age)")
}