I have a bunch of classes derived from UIView derivatives. Those clases also conform to protocol commonFunctions. I store them for further use in a dictionary, like this:
var elementsStore:[String:UIView & commonFunctions]
and protocol commonFunctions goes like this:
protocol commonFunctions:AnyObject {
…
func setTheme(theme:Theme);
…
var overrideMargins: (left:CGFloat, right:CGFloat, top:CGFloat, bottom:CGFloat) {get};
…
}
I highlighted those parameters because, due to their presence, the protocol cannot be changed to @objc instead of AnyObject. That is one of the solutions I've found so far, but to no avail.
Now I need to get rid of the dictionary.
I know I can store a weak reference to such variables like this
weak var var1:(UIView & commonFunctions)?
But since there are different types of classes I need to change, I am implementing a generic. So it goes like this:
class WeakDictionaryData<Type:AnyObject>
{
weak var value:Type?;
init(_ value:Type)
{
self.value=value;
}
}
But when I try to use the generic with the same data type UIView & commonFunctions, like this:
var var2:WeakDictionaryData<UIView & commonFunctions>
I get this error.
'WeakDictionaryData' requires that 'UIView & commonFunctions' be a class type.
I know that protocol commonFunctions by itself does not conform to a class type, BUT UIView does. (The not so common declaration UIView & commonFunctions means that such variable conforms to both the class AND the protocol, so they will always be a class type). The only way I've found to remove that error is removing the AnyObject constraint from the generic, but then I get this error in the weak declaration:
'weak' must not be applied to non-class-bound 'Type'; consider adding a protocol conformance that has a class bound.
After a long time experimenting changes, it seems to me that the problem is the constraint in the generic, otherwise the declaration out of the generic would throw an error. But I haven't found a way to describe that constraint for the generic.
So I'm stuck.
Any help would be really appreciated.