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!

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 in Swift Helper Methods

Why didn't he just enemy.position == point?

Why didn't he just enemy.position == point? As such if point == enemy.position{....

2 Answers

Steven Parker
Steven Parker
228,001 Points

A value equality test ("==") does not by default compare the properties of complex objects. And reference equality ("===") only checks if both references are for the same object.

So for the purpose of this method, it's necessary to compare the properties individually.

It would probably be overkill for this situation, but a type can declare to other code that it supports value equality by conforming to the Equatable protocol. For details, see the swift reference guide page on Equatable.

Oh, so its like java? I would need to create a "equals" method that overrides the default?

Steven Parker
Steven Parker
228,001 Points

You might only need to declare "Equatable conformance". See the amendment to my answer: :arrow_heading_up: