Hi guys. Sorry about the confusing title.
Here is the situation.
I have a protocol defining two methods about encoding.
- One for
Codable
objects:
func set<Object: Codable>(_ object: Object?, by key: Key) throws
- The other one for
NSObject
s that areNSCoding
:
func set<Object: NSObject & NSCoding>(_ object: Object?, by key: Key) throws
When I pass in an object that is an NSObject
, Codable
and NSCoding
, it prefers the Codable
function:
class Annoying: NSObject, Codable, NSCoding {
func encode(with coder: NSCoder) { }
required init?(coder: NSCoder) { }
override init() { }
}
cache.set(Annoying(), by: "key")
My questions are
Why won't this cause ambiguity error?
Is this a defined behavior? Is this guaranteed in future Swift versions?