I have an @objc
protocol with this implementation:
@objc public protocol Document: class {
var data: Data? { get }
var image: UIImage? { get }
func generatePreview()
}
And I'm trying to use it in a [Document: Int]
dictionary, but naturally I get this error:
Type 'Document' does not conform to protocol 'Hashable'
The problem is that I don't know how to make it conform Hashable
, since it is an @objc
protocol and Hashable
is only available in Swift. If I try to make it conform Hashable
, I get this error:
@objc protocol 'Document' cannot refine non-@objc protocol 'Hashable'
This protocol is used as a property in an @objc
method, which I want to keep as an '@objc' method since it is part of a @objc
delegate protocol.
This protocol looks like this:
@objc public protocol MyClassDelegate: class {
@objc func methodOne(parameter: [Document: Int])
}
Any idea?
You can use [AnyHashable: Int]
which is a type-erased hashable value.
I assume you have the protocol called Document:
@objc public protocol Document: class {
var data: Data? { get }
var image: UIImage? { get }
func generatePreview()
}
And your protocol methodOne will be accepting [AnyHashable: Int]:
@objc public protocol MyClassDelegate: class {
@objc func methodOne(parameter: [AnyHashable: Int])
}
Its kinda complicated question, after some research i agree with @Rob Napier
don't use protocols as keys in dictionaries. It never goes well and all the workarounds are a pain.
I might not answered perfectly but hopefully that should make the errors goes away.
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