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
Mark El-Lamaa
1,634 PointsThis isn't technically from treehouse but some help will be appreciated...
struct Size { var width = 0.0, height = 0.0 } struct Point { var x = 0.0, y = 0.0 }
Rect {
var origin = Point()
var size = Size()
init() {}
init(origin: Point, size: Size) {
self.origin = origin
self.size = size
}
}
my question is what is the point of the lines self.origin=origin and self.size=size at the end? Something got to do with initialisation i know but doesn't the init(origin: Point, size: Size) initialise that code already?!?
1 Answer
Stone Preston
42,016 Pointsan initializer is just a special function. you pass in arguments, and those arguments are used to set the values of properties of that class/struct.
in your Rect initializer, you initialize the origin and size properties using the arguments that are passed in to the function.
init(origin: Point, size: Size) {
self.origin = origin
self.size = size
}
when the init method is called, you pass in the two origin and size arguments. then, the values of the origin and size properties of the rect struct are set using those values.
since the init parameters are named the same as the properties (the properties are named origin and size, but so are the parameters), you have to use self.origin to reference the property, since origin refers to the paramter
Mark El-Lamaa
1,634 PointsMark El-Lamaa
1,634 Pointsthank you makes sense now