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

Amelia Boli
Amelia Boli
1,953 Points

Why would you set a default value for price when you initialize it later in the class?

I just don't understand why price is set to 0.0 when it is declared when price is part of the init method further down in the same class.

3 Answers

David Clausen
David Clausen
11,403 Points

He's just walking through expanding the class to accept initializes so you can have a custom price. You can absolutely assign it in the init.

class Product {
    var Price: Double

    init (price: Double){
        self.price= price
}

This should be acceptable and fine to use. Instead of deleting and going backwards it was easy to move forward with creating a initializer.

Amelia Boli
Amelia Boli
1,953 Points

Are you saying that he simply didn't delete the 0.0 because he didn't want to go back and do so? There are several more videos which use this class and he never removes the 0.0 even though he always initializes the price after the first example. Is this a best practice?

David Clausen
David Clausen
11,403 Points

There is no cut and dry anwser to that. Apple documentation seems to suggest constructed initialization.

Honestly it depends on what you are doing with the variable. Lots see it in Object Oriented the purpose of the variable dictates where you initialize it.

If a variable in a class is immutable (never changes) amongst all the class then I do inline initialization. If its simply set to a default value among all instance of the class I do inline. If its going to be changed right away I do Do constructor.

In this case I think what he did is not considered beat practice. Since it's a price and you will always change it and the constructor requires the input of a price to create the instance then there is no need to set it to zero.

Hello,

I have the same question. But I do not understand that if the variable "price" is changed to a constant, he can still initialize it with the designated initializer. When I do this, I am getting an error: Cannot assign to 'price' in 'self'. It is only possible to have a default value when the price is a variable, not a constant. Isn't this correct?

class Product {
   let title: String
   let price: Double = 0.0

   init(title: String, price: Double) {
      self.title = title
      self.price = price
   }
}

So as the code in the example, I will have the error on the line 7.

Sain-R Edwards Jr.
Sain-R Edwards Jr.
1,853 Points

I think you're right Robert Szabo. I receive the same error when I change price to a constant. Weird.

Nic Laughter
Nic Laughter
2,692 Points

Came to the comments to find this. Glad I'm not missing something!

Nic Laughter
Nic Laughter
2,692 Points

NEVER MIND! I just realized you did the same thing I did, which was to neglect changing the class members from constants to variables. having "price" as a let, I deleted the 0.0 to get rid of the error, but then when Amit tells you to type "quadcopter.price = 199.99" I was getting another error, and that's when I realized the problem. Doing this will enable you to change the values of the members of class Product with subsequent commands.