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

Computed properties challenge.

I don't know how to complete this challenge. Please help.

Temperature.swift
class Temperature {
    var celsius: Float = 0.0
    var fahrenheit: String {
      return (celsius*1.8)+32
    }
  init (celsius: Float, fahrenheit: String){
    self.celsius=celsius
    }
}

4 Answers

Hello again, Jac Wynn & Rufaro Makoroni :

No problems on my end when I use XCode, with the advised changes I recommended earlier today:

// Playground - noun: a place where people can play

import UIKit

class Temperature {
  var celsius: Float = 0.0
  var fahrenheit: Float {
    return (celsius * 1.8) + 32
  }
    init(celsius: Float){
    self.celsius=celsius
  }
}


  let a : Temperature = Temperature(celsius: 23.0)

println(a.fahrenheit)

Hi, Rufaro Makoroni:

You have the wrong type associated with your fahrenheit value; it should be a float.

What you have currently the compiler will complain since the return value of the operations based on the operands (* & +) will not change things to return a String; A Float is what you meant to use, I'm assuming?

class Temperature {
  var celsius: Float = 0.0
  var fahrenheit: Float {
    return (celsius*1.8) + 32
  }
  init (celsius: Float){
    self.celsius = celsius
  }
}

Hi, Kevin Lozandier. I keep getting an error even after writing the code as you have done above.

That's odd; Can you provide me an exact error? I usually code Swift on the latest version of Swift the latest version XCode provides for the sake of debugging; I'm able to use the class as a Swift developer would normally expect.

finally got it. Thank you :)

Also note, of course the fahrenheit variable—since it's a computed property—is read-only: Your init will never work as written.

A user of your class cannot edit or initialize a new instance of Temperature with fahrenheit directly—if they were able to, it would defeat the purpose of a computed property being of value towards the property being used accurately in an application.

Im still getting a error with my code and I'm not sure what I have wrong. Could somebody help me out??

class Temperature {
    var celsius: Float = 0.0
    var fahrenheit: Float {
      return celsius * 1.8 + 32
    }
    init (celsius: Float) {
      self.celsius = celsius
    }
}

Edited your answer to have proper code highlighting: Please surround your code with three, consecutive backticks/grave characters (the character next to the 1 key on most traditional keyboards) with the first set of backticks labeled with the language your code snippet's syntax is adhering to.