-
Notifications
You must be signed in to change notification settings - Fork 23
/
Contents.swift
50 lines (34 loc) · 1.17 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
import UIKit
// Exercise - Memberwise and Custom Initializers
// --------- Memberwise Initializers
struct GPS2 {
var latitude: Double
var longitude: Double
}
let somePlace2 = GPS2(latitude: 51.514004, longitude: 0.125226)
print(somePlace2.latitude)
print(somePlace2.longitude)
// ---------
struct Book2 {
var title: String
var author: String
var pages: Int
var price: Double
}
var favoriteBook2 = Book2(title: "Harry Potter", author: "J. K. Rowling", pages: 223, price: 14.99)
print("My favorite book is called \(favoriteBook2.title), written by \(favoriteBook2.author). It has \(favoriteBook2.pages) pages and costs £\(favoriteBook2.price)")
// --------- Custom Initializers
struct Height {
var heightInInches: Double
var heightInCentimeters: Double
init(heightInInches: Double) {
self.heightInInches = heightInInches
self.heightInCentimeters = heightInInches * 2.54
}
init(heightInCentimeters: Double) {
self.heightInCentimeters = heightInCentimeters
self.heightInInches = heightInCentimeters / 2.54
}
}
var someonesHeight = Height(heightInInches: 65)
print(someonesHeight.heightInCentimeters)