Well, that is strange.
Why does this happen?
Because:
-
On iOS, the compiler is using -[NSDictionary isEqualToDictionary:]
. Its parameter is of type NSDictionary
, which gets imported as [AnyHashable: Any]
.
-
On macOS the compiler finds the -isEqualTo:
method, added by a category on NSObject
by <Foundation/NSScriptWhoseTests.h
. Its parameter is nullable id
, which gets imported as Any?
.
That header is associated with Foundation’s AppleScript support, and thus isn’t present on iOS.
What’s weird is that the compiler chooses to use the category method. The dictionary method seems more specific. However, Swift’s overload resolution rules are both complex and not documented precisely, so it’s hard to say whether it’s right or wrong. If you want to ‘language lawyer’ that, I recommend that you bounce over to Swift Forums.
Can I use NSDictionary.isEqual(_:) instead?
Yes. Well, it’s NSObject.isEqual(_:)
, but NSDictionary
overrides it with the right logic.
The whole -isEqualToXxx:
stuff always confused me. I’m not really sure why it exists. Sure, it’s slightly faster, but how much does that actually buy you?
Alternative you could use Swift’s ==
, because for NSObject subclasses that dispatches through -isEqual:
.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"