Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

jon kelson
jon kelson
5,149 Points

explain 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
Alexander Smith
10,476 Points

Yea 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

jon kelson
jon kelson
5,149 Points

Thanks 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
Alexander Smith
10,476 Points

To 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

Alexander Smith
Alexander Smith
10,476 Points

An 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
jon kelson
5,149 Points

thanks 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)