[swift-users] Equality is broken, isn't it?
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)) !=
(a as NSString).hashValue == b.hashValue
a.hashValue == (b as String).hashValue
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