Some time ago I worked on a proof-of-concept which used a type-safe units conversion library in swift (that had been before
Foundation introduced their own units support). The library had measurements composed of a
Double amount and a unit, e.g. “1.5 meters”. To compare measurements in tests with
XCTAssertEqual, it’s necessary to conform to
Equatable and implement the
== function. But how do you specify the accuracy to compare doubles in this case?
Warning: this is a quick and dirty hack that shouldn’t really be used in production. Try to come up with a better design for your code to avoid it if possible.