Just a sanity check with the community before I file a radar:
In a .h Obj-C file:
@protocol myProto <NSObject>
@end
In a .swift file (that has access to the above protocol definition via bridging header):
class myClass {
// This line compiles fine
var dictOne: [NSObject:Int]?
// This line fails with "Type 'myProto' does not conform to protocol 'Hashable'"
var dictTwo: [myProto:Int]?
}
Inspection of the NSObject class reveals that it (or the NSObjectProtocol that it maps to) does not implement the hashValue method required by the Hashable protocol, nor does it explicity adopt it.
So, somewhere behind the scenes the NSObject is being flagged as Hashable despite this, but does not extend to protocols that adopt NSObject/NSObjectProtocol.
Have I got a bug or am I missing something?
:) Teo
Additional info:
The documentation suggests that:
==
.Hash Values for Dictionary Key TypesA type must be hashable in order to be used as a dictionary’s key type—that is, the type must provide a way to compute a hash value for itself. A hash value is an Int value that is the same for all objects that compare equal, such that if a == b, it follows that a.hashValue == b.hashValue.
All of Swift’s basic types (such as String, Int, Double, and Bool) are hashable by default, and all of these types can be used as the keys of a dictionary. Enumeration member values without associated values (as described in Enumerations) are also hashable by default.
NOTE You can use your own custom types as dictionary key types by making them conform to the Hashable protocol from Swift’s standard library. Types that conform to the Hashable protocol must provide a gettable Int property called hashValue, and must also provide an implementation of the “is equal” operator (==). The value returned by a type’s hashValue property is not required to be the same across different executions of the same program, or in different programs. For more information about conforming to protocols, see Protocols.
NSObjectProtocol doesn't inherit from Hashable .
You can use any type that conforms to the Hashable protocol in a set or as a dictionary key. Many types in the standard library conform to Hashable : Strings, integers, floating-point and Boolean values, and even sets are hashable by default.
In Swift, a Hashable is a protocol that provides a hashValue to our object. The hashValue is used to compare two instances. To use the hashValue , we first have to conform (associate) the type (struct, class, etc) to Hashable property.
To conclude, a hashable protocol allows us to create custom types that can be compared for it's equality using it's hashValue . In this way we are able to use our custom type in a set or as a key in a dictionary while ensuring that it has a unique value.
NSObjectProtocol
doesn't inherit from Hashable
. That's the crucial problem here.
It can't actually inherit from Hashable
because Hashable
requires a method called hashValue
while NSObjectProtocol
requires a method called hash
.
On the other hand, NSObject
class can implement both NSObjectProtocol
and Hashable
.
The same problem happens with Equatable
.
Edit:
There is another more subtle problem. You cannot use a protocol somewhere where Equatable
is expected, you always need to use a class type or a value type that adopts Equatable
. The reason is that it's not enough for a key to adopt Equatable
, all the keys in the dictionary have to be equatable with each other.
For example, if you have a class A
and a class B
, both conforming to Equatable
, then you can compare instances of A
with other instances of A
and you can compare instances of B
with other instances of B
but you cannot compare instances of A
with instances of B
. That's why you cannot use instances of A
and instances of B
as keys in the same dictionary.
Note that every NSObject
is equatable with any other NSObject
, so NSObject
is an allowed type for keys in a dictionary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With