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 trialjon kelson
5,149 Pointsexplain init methods in basic terms please
Hi Please could someone explain init methods a little clearer please . I did the 1st swift coarse and thought I understood until I started swift.2 object oriented swift ! .
many thanks
2 Answers
Alexander Smith
10,476 PointsYea I remember that one. Well its the same concept just a lot more thrown in and it involve two classes. The way I went to understand it was just by thinking it as a chain just like any other, just a really long one lol. I just went into xCode and clicked on each part making it underline to where it linked to but ill elaborate.
struct Point {
let x : Int
let y : Int
// initializer
init(x: Int, y: Int) {
self.x = x
self.y = y
}
K heres your Point class that let position uses and...
class Tower {
let position: Point
var range: Int = 1
var strength: Int = 1
init(x: Int, y: Int) {
self.position = Point(x: x, y: y)
}
}
Part of the Tower class. Just like before, self.position is linked to let position: Point in Tower class. Then since position is of type Point you gotta write that like an instance of type Point. Point is written like Point(x: Int, y: Int) but since you gotta link it to your init you go one step further. x and y in your init is linked to x and y standing in for Int values in Point.
I know its a mouthful but thats what it boils down to. I used xCode to work it out in my head better
Alexander Smith
10,476 PointsAn init method is basically used to create a template to use when creating an instance. Example...
class Example {
var name: String
init(name: String){
self.name = name
}
}
So in this Example class we have one var to initialize. I like to think of init methods as links in a chain. The self.name is linked to var name: String. name that self.name = , is linked to name in the init parameter. when you create an instance of Example class you do it like this...
let ex = Example(name: "Alex")
This parameter is a direct reflection of your init method. Your init method is what you use to create an instance. Objects like a struct is given a default init so you dont need to write one
jon kelson
5,149 Pointsthanks Alex , sorry should have said Its more this sort of Init that was confusing me in swift.2 object oriented swift .
Many thanks
class Tower {
let postion: Point
var range : Int = 1
var strength: Int = 1
init(x: Int, y: Int) {
self.postion = Point(x: x, y: y)
jon kelson
5,149 Pointsjon kelson
5,149 PointsThanks very much Alex . That's helped me to understand it better . Thanks
Just one last question if you don't mind . Is this point struct used a lot in this type of game concept when people make games ? Or is it just something treehouse has made up ?
Alexander Smith
10,476 PointsAlexander Smith
10,476 PointsTo my understanding this is the basic object for a point. I have seen it in a couple places when making anything that relies on a coordinate plane. It can be expanded for the z plane as well for 3D