Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in swift playgrounds In the Height struct below, height is represented in both inches and centimeters. However, if heightInInches is changed, heightInCentimeters should also adjust

in swift playgrounds

In the Height struct below, height is represented in both inches and centimeters. However, if heightInInches is changed, heightInCentimeters should also adjust to match it. Add a didSet to each property that will check if the other property is what it should be, and if not, sets the proper value. If you set the value of the other property even though it already has the right value, you will end up with an infinite loop of each property setting the other.

Create an instance of Height and then change one of its properties. Print out the other property to ensure that it was adjusted accordingly.

says i have a problem with Height, says the declaration is wrong

struct Height {

var heightInInches: Double{

willSet {

print("About to set heightInINches to \(15)")

}

}

didSet{

if heightInInches > oldValue {

print ("Added \(heightInInches - oldValue) inches")

}

}

var heightInCentimeters: Double{

willSet{

print("About to set heightInCentimeters to \(20)")

}

didSet{

if heightInCentimeters > oldValue {

print("Added \(heightInCentimeters - oldValue) centimeters")

}

}

}

init(heightInInches: Double) {

self.heightInInches = heightInInches

self.heightInCentimeters = heightInInches*2.54

}

init(heightInCentimeters: Double) {

self.heightInCentimeters = heightInCentimeters

self.heightInInches = heightInCentimeters/2.54

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

1 . Television News channels importantance of our Life pattern ?

Answered: 1 week ago

Question

1. How is the newspaper help to our daily life?

Answered: 1 week ago

Question

1. Prepare a short profile of Mikhail Zoshchenko ?

Answered: 1 week ago

Question

What is psychology disorder?

Answered: 1 week ago