0

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.

Alfredo Meraz
  • 139
  • 1
  • 7
  • Is this the same issue as https://stackoverflow.com/q/51541353/3141234 ? – Alexander Jun 01 '22 at 21:35
  • Not related, but instead of passing UIView & commonFunctions everywhere, give it a type alias, and use that. – Tushar Sharma Jun 01 '22 at 21:36
  • dont treat ui as data. just listen to a veteran. dont do it. – vikingosegundo Jun 01 '22 at 21:38
  • @Alexander no, it's not the same issue. Over there is the recurring question about a protocol conforming to itself. Here I have a class along with the protocol, but always a class. It should pass the generics constraint. – Alfredo Meraz Jun 02 '22 at 15:06
  • @TusharSharma it's a loooong project. I already got used to it. But thanks anyway – Alfredo Meraz Jun 02 '22 at 15:09
  • @vikingosegundo data and UI are long intertwined by the very nature of this project – Alfredo Meraz Jun 02 '22 at 15:12
  • @AlfredoMeraz I got a similar sense from reading the answers. I think they're mis-diagnosing the issue, even in the other question. I think you might get some better success getting help for this kinda thing from forums.swift.org. I'd suggest you slim down the example though. – Alexander Jun 02 '22 at 15:52

0 Answers0