[swift-users] Equality is broken, isn't it?

Jens Alfke jens at mooseyard.com
Wed Jul 6 13:13:07 CDT 2016

> On Jul 6, 2016, at 9:54 AM, Alexey Komnin via swift-users <swift-users at swift.org> wrote:
> Here is the code:
>    let a: String = "abc"
>    let b: NSString = "abc"
>    assert(a == b)
>    assert(a.hashValue == b.hashValue, "a.hashValue(\(a.hashValue)) !=
> b.hashValue(\(b.hashValue))”)

Try comparing
	(a as NSString).hashValue == b.hashValue
	a.hashValue == (b as String).hashValue

It’s true that equal objects should have equal hash codes, but that really only applies when the objects have the same type so there’s no implicit coercion going on. For example, in scripting languages where “1” == 1 (pretty sure this is true of JavaScript and Perl, for example) it’s probably not true that “1”.hashCode == 1.hashCode.

In a Swift Dictionary, if you give the item type as String or NSString then everything’s converted to the same type when added, so hashCode will work as you expect. And if you use a Dictionary<AnyObject>, there won’t be any implicit String<->NSString conversions going on, so if you added both your ‘a’ and ‘b’ objects I believe they would be stored separately as distinct keys.

(The moral is that the distinction between String and NSString is kind of messy, unfortunately. But the implicit conversion has been pretty important for Swift adoption given how ubiquitous NSString is in Cocoa APIs and existing apps.)


More information about the swift-users mailing list