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 Classes and Objects Classes and Their Methods

Philip Micallef
Philip Micallef
1,962 Points

error: cannot assign to 'let' value. Im not sure want I'm doing wrong

Not sure what I'm doing wrong, error: cannot assign to 'let' value height . error: cannot assign to 'let' value width. however both are variables. I tried with and without declaring values to height and width

Button.swift
class Button {
  var width: Double = 0.0
  var height: Double = 0.0

  init(width:Double, height:Double){
    self.width = width
    self.height = height
                    //Steve says "close the curly brace here"
  func incrementBy(points: Double) {
     height = (height + points)
     width = (width + points)
    }
  }
}

6 Answers

Right OK - once I realised it was a code challenge (sorry I was being dim!!) I came up with this code:

class Button {
  var width: Double
  var height: Double

  init(width:Double, height:Double){
    self.width = width
    self.height = height
  }

  func incrementBy(points: Double){
    self.width += points
    self.height += points
  }
}

Which works fine. SO does:

class Button {
  var width: Double
  var height: Double

  init(width:Double, height:Double){
    self.width = width
    self.height = height
  }

  func incrementBy(points: Double){
    self.height = self.height + points
    self.width = self.width + points
  }
}

The issue with your original code is that you a missing a closing curly brace after the init function. Sorry, I missed that earlier!

Steve.

I've added a comment to your post - you probably need to add some self. in there too.

I think your incrementBy() uses of height and width needs to have self. added prior to them.

That should fix the problem.

Steve.

Philip Micallef
Philip Micallef
1,962 Points

It still doesn't want to compile although it doesn't show me any errors. I do really apprentice you trying to help me Steve.

What errors are you getting?

Hi Philip,

This works in my Playground:

class Button {
    var width: Double = 0.0
    var height: Double = 0.0

    init(width:Double, height:Double){
        self.width = width
        self.height = height
     } 
     func incrementBy(points: Double) {
        self.height = (self.height + points)
        self.width = (self.width + points)
     }
}

Steve.

It works without the = 0.0 on both variables too.

Philip Micallef
Philip Micallef
1,962 Points

I literally copied and pasted your code in the task challenge and it still doesn't want to compile.

OK - let's try to replicate this issue. I'll open Xcode!

What have you got set up? A project called Button with one file containing the class?

What's the compiler crying about? Does it come up with any error messages?

It's a code challenge - sorry! Let me have a look at that first ...

Philip Micallef
Philip Micallef
1,962 Points

Im trying to complete a Code challenge from Object - Oriented Swift , Classes and their Methods. There are no errors showing in the complier , but it keeps telling me " Bummer! Your code could not be compiled. Please click on "Preview" to view the compiler errors."

Philip Micallef
Philip Micallef
1,962 Points

Oh wow, I feel stupid now. haha. Thank you very very much Steve , it works finally. I really appreciate it.

No problem - we all make mistakes; that's why we're using Team Treehouse!