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

In need of some definitions for syntax terms

These are the terms I need definitions of (and yes, I understand that I could Google them or watch back some videos, but I am asking this community as I'm hoping for some simple, getting-to-the-point-fast answers)

1) Intializers 2) Self 3) Parameters 4) 'Calling' something

Thank you for reading and/or answering.

1 Answer

Initialization is basically preparing an instance of a class, structure, or enumeration for use. This involves setting an initial value for each stored property on that instance and performing any other setup or initialization that is required before the new instance is ready for use. Let's take a look at the example below.

I created a class, then after setting the properties I initialize the class. In my init statement I am setting name, playerNumber and CurrentPlayer.

import Foundation
import SpriteKit

class Player  {
    let name: String
    let playerNumber: Int
    var isCurrentPlayer: Bool
    var gamePieces: Set<GamePiece> = []

    init(name:String,playerNumber: Int, isCurrentPlayer: Bool){
        self.name = name
        self.playerNumber = playerNumber
        self.isCurrentPlayer = isCurrentPlayer
    }
}

extension Player: Printable {
    var description: String {
        get {
            return "Player \(playerNumber)"
        }
    }
}