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 trialPhilip Micallef
1,962 Pointserror: 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
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
Steve Hunter
57,712 PointsRight 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.
Steve Hunter
57,712 PointsI think your incrementBy()
uses of height
and width
needs to have self.
added prior to them.
That should fix the problem.
Steve.
Philip Micallef
1,962 PointsIt still doesn't want to compile although it doesn't show me any errors. I do really apprentice you trying to help me Steve.
Steve Hunter
57,712 PointsWhat errors are you getting?
Steve Hunter
57,712 PointsHi 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.
Steve Hunter
57,712 PointsIt works without the = 0.0
on both variables too.
Philip Micallef
1,962 PointsI literally copied and pasted your code in the task challenge and it still doesn't want to compile.
Steve Hunter
57,712 PointsOK - 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?
Steve Hunter
57,712 PointsIt's a code challenge - sorry! Let me have a look at that first ...
Philip Micallef
1,962 PointsIm 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
1,962 PointsOh wow, I feel stupid now. haha. Thank you very very much Steve , it works finally. I really appreciate it.
Steve Hunter
57,712 PointsNo problem - we all make mistakes; that's why we're using Team Treehouse!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsI've added a comment to your post - you probably need to add some
self.
in there too.