(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.)
On Jul 6, 2016, at 9:54 AM, Alexey Komnin via swift-users <firstname.lastname@example.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)) !=