-
Notifications
You must be signed in to change notification settings - Fork 23
/
Contents.swift
48 lines (40 loc) · 1.11 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
import UIKit
// Exercise - Computed Properties and Property Observers
struct Rectangle {
var width: Int
var height: Int
var area: Int {
return width * height
}
}
var rectangle = Rectangle(width: 4, height: 5)
print(rectangle.area)
// ---------
struct Height2 {
var heightInInches: Double {
didSet {
if heightInCentimeters != heightInInches*2.54 {
heightInCentimeters = heightInInches*2.54
}
}
}
var heightInCentimeters: Double {
didSet {
if heightInInches != heightInCentimeters/2.54 {
heightInInches = heightInCentimeters/2.54
}
}
}
init(heightInInches: Double) {
self.heightInInches = heightInInches
self.heightInCentimeters = heightInInches*2.54
}
init(heightInCentimeters: Double) {
self.heightInCentimeters = heightInCentimeters
self.heightInInches = heightInCentimeters/2.54
}
}
var height = Height2(heightInCentimeters: 20)
print(height.heightInInches)
height.heightInCentimeters = 30
print(height.heightInInches)