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 Swift 2.0 Protocols Creating Flexible Objects Using Protocols Protocol Oriented Programming Part 2

Max Howarth
Max Howarth
2,605 Points

"var parameters are depreciated and will be removed in Swift 3" - how does this change our example?

In this example, we use a var parameter player of type "PlayerType" in the attack func.

I am getting an error saying that var parameters will be depreciated in Swift 3, and they recommend removing the "var" from the function, but doing so creates an error as they say "player is a let-type".

How would we accomplish the same function without using a var parameter?

class Enemy: PlayerType, Destructable , Attackable, Movable {
    var position: Point
    var life: Int = 10
    var strength: Int = 5
    var range: Int = 2

    required init(point: Point) {
        self.position = point
    }

    func decreaseLife(factor: Int) {
        self.life -= factor
    }

    func attack(var player: PlayerType) {
        player.life = player.life - strength
    }

    func move(direction: Direction, distance: Int) {
        switch direction {
        case .Up: position.y += 1
        case .Down: position.y -= 1
        case .Left: position.x -= 1
        case .Right: position.x += 1
        }
    }
}

2 Answers

Stone Preston
Stone Preston
42,016 Points

you could try something like this:

func attack(player: PlayerType) {

        var player = player
        player.life = player.life - strength
    }

source: http://stackoverflow.com/questions/36164973/var-parameters-are-deprecated-and-will-be-removed-in-swift-3

I cant test anything right now but just going by what the stack overflow comments show, that might work

Dalisson Figueiredo
Dalisson Figueiredo
7,731 Points

That is because the syntax will change on following updates of swift, so get used to the code the moderator has provided: func attack(player: PlayerType) { var player = player player.life = player.life - strength }