[swift-users] Workarounds for SR-6039?
Marc Palmer
marc at anyware.co.uk
Sun Jan 7 09:30:50 CST 2018
Hi,
It’s hard to be 100% certain because of bugs.swift.org being down currently, but I believe I have hit this bug:
https://bugs.swift.org/browse/SR-6039 "A `protocol A: class` is not any `AnyObject`”
This is marked as a duplicate of a presumably very old issue judging by its low number:
https://bugs.swift.org/browse/SR-55 "non- at objc protocol existentials do not conform to their own protocol type"
So, I have a protocol:
public protocol ActionAuditorObserver: AnyObject {
func actionAuditorInserted(entries: [ActionAuditEntry])
}
…where ActionAuditEntry is a struct.
Trying to use a generic collection of my own for these, of the form:
class ObserverSet<T: AnyObject> {
…
}
Using:
let observers = ObserverSet<ActionAuditorObserver>()
…results in "'ActionAuditorObserver' is not convertible to ‘AnyObject’”.
I think this is indeed SR-6039, which makes me really sad as it implies this is not going to be fixed imminently.
I am trying to write this as a pure-swift framework as so far have succeeded. If I add @objc to the protocol as a workaround for this, I then can no longer have the observer pass in an array of structs.
So then I have to change the struct to a class, and I’m pushed farther away from my goals :(
Is there anything I am missing, or a better way to workaround this?
I have to have the ObserverSet require AnyObject because it needs to do identity checking when removing them. Equatable does not make sense for observers. Losing the struct and forcing @objc feels like such a bitter pill for something so simple.
Thanks in advance
Marc
More information about the swift-users
mailing list