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
Bryan Elliott
6,835 PointsHelp me Initialize All the Things !
I need help understanding initializing because I'm not exactly sure of what I'm doing nor do I possess the technical acumen of when I should be using them. I have a VERY light idea of when I should be using them. I'm not even sure what or why it exist. I just know I need them. Convince initializers are also confusing.
Are initializers put in place to give default values for your classes as a starting point ?
I went on ahead and re-watched some of Amit's videos and re-wrote the code. My questions are asked inline
// These are the initial values, correct ? Are these the starting points for an object ?
class Product {
let title: String
var price: Double = 0.0
// 1st initializer - Does this set a default value of title and price to "title" and "price" ?
// This doesn't make sence to me because price should be a double, right ?
// If we have this, why do we need to declare initial values above ? @ starting points
// code like this seems redundant to me. I feel like we are saying
//"This particular classe's title and price are equal to title and price"
// Can anyone elaborate on this for me ?
init (title: String, price: Double){
self.title = title
self.price = price
}