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

Benjamin Lim
Benjamin Lim
2,747 Points

Type 'Enemy' does not conform to protocol 'Attackable' I'm not sure what's wrong, but i copied and paste from the gist

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
    }
}

}

Steven Deutsch
Steven Deutsch
21,046 Points

Can I see your Attackable protocol please? Thanks :)

2 Answers

Berry Loeffen
Berry Loeffen
4,303 Points

The struct Point needs its properties set to var in stead of let. Also some of the protocols names are spelled a little differently in the code snippets compared to what we wrote with the instructor, so check if the spelling is the same

Dustin Spengler
Dustin Spengler
5,480 Points

maybe try taking out the "var" from the attack function parameters? even if thats not the issue it will be deprecated soon.

Daniel Cohen
Daniel Cohen
5,785 Points

If I remove 'var' from the attack function parameter then I get an error "cannot assign to property 'player' is a 'let' constant". If I leave it in then i get a warning that var parameters are deprecated and will be removed in Swift 3. What am I supposed to do if I don't use a var parameter?