Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Object-Oriented Swift Properties Computed Properties

challenge 1 :\ unknown error!

good evening every one :) i tried to write the code that geven in the challenge but there still a mistake i wrote it as this :

class Temperature {
    var Celsius: Float = 0.0
    var Fahrenheit: Float {
        return (Celsius * 1.8) + 32
    }
    init ( Celsius: Float){
        self.Celsius = Celsius
    }
}
let tump = Temperature (Celsius: 56.6)
tump.Fahrenheit

it showes me this error : You forgot to add a computed property named fahrenheit!

3 Answers

HI there,

This is a tricky one, yes. Here's one solution for it:

class Temperature {
    var celsius: Float = 0.0
    var fahrenheit: Float {
        get {
            return (celsius * 1.8) + 32
        }
        set {
            celsius = (newValue - 32) / 1.8
        }
    }
}

I don't think you need the initializer in there for this challenge. You must compute the fahrenheit variable by backward engineering from the value of celsius.

ok thank you Steve , but i have a qustion here , what did i compute in my code ??

HI there,

You mixed a variable declaration, var Fahrenheit: Float with a method. It wouldn't compile to anything, I wouldn't think.

Steve.

P.S. Only class names should be capitalised, I think, so your stored properties should be celsius and fahrenheit.

ok one last question :) in the challenge he asked to assign read-only that means we have to do getter method without setter ! right ??

and i want to mention something i did what Mr.Amit done in previous video . . .

class fuirnature : product {
    let hight: Double
    let width : Double
    let length : Double

    var surfaceArea: Double {
        return length * hight
    }

    init ( title: String , price: Double , hight: Double , width: Double , length: Double) {
        self.hight = hight
        self.width = width
        self.length = length
        super.init (title: title , price: price)
    }
}
let table = fuirnature (title: "coffee Table", price: 23, hight: 56, width: 67, length: 78)
table.surfaceArea 

is it wrong ?! i mean i didn't anderstand what you mean by mix variable declaration with method!

thank you :)

SANGEETHA KURUNTHAIAH
SANGEETHA KURUNTHAIAH
1,947 Points

Hi Nour, This challenge can be cracked as above..

 var celsius: Float = 0.0
  var fahrenheit: Float {
   return (celsius * 1.8) + 32   }

no need to use init here..