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 Designated Initializer

Joshua Rystedt
Joshua Rystedt
4,086 Points

Cannot Assign to Width to Self

When completing this challenge I followed Amit's example and passed the challenge section but XCode (6.3.2) is giving me the following error twice on lines 5 & 6: ! Cannot assign to 'width' in 'self'

Anyone know what is going on here?

class Button {
    let width: Double = 1.00
    let height: Double = 1.00
    init(width: Double, height: Double) {
        self.width = width
        self.height = height

1 Answer

Hi Joshua

That is because you set your width and height to be constants and gave them value of 1.00, then with init you try to assign them new value which is not possible because they are constants.

You need to change them to be variables (use var instead of let) or delete predefined value of 1.00