Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSObject is Hashable but a protocol that adopts NSObject is not?

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:

  • A dictionary's key type's only requirements is that it's Hashable and that it implements ==.
  • You can indeed use a protocol.
Hash Values for Dictionary Key Types

A 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.

like image 804
Teo Sartori Avatar asked Jul 24 '14 15:07

Teo Sartori


People also ask

Does Nsobject conform hashable?

NSObjectProtocol doesn't inherit from Hashable .

Can a protocol conform to 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.

What does it mean to be hashable Swift?

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.

What is the use of hashable protocol?

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.


1 Answers

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.

like image 150
Sulthan Avatar answered Sep 23 '22 16:09

Sulthan