Checking conforms protocol with associatedtype in Swift

Viewed 2307

How can I do a check object conforms to protocol 'Representable' in a similar situation?

protocol Representable {
    associatedtype RepresentType
    var representType: RepresentType { get set }
}

class A: UIView, Representable {
    enum RepresentType: String {
        case atype = "isa"
    }
    var representType: RepresentType = .atype
}

class B: UIView, Representable {
    enum RepresentType {
        case btype(value: String?)
    }
    var representType: RepresentType = .btype(value: nil)
}

let obj = A()
if let obj = obj as? Representable {  <<<<<<<<<<<< error
    obj.representType = A.RepresentType.atype
}

Error: Protocol 'Representable' can only be used as a generic constraint because it has Self or associated type requirements if let obj = obj as? Representable

It is important that each class implements its enumeration of types of representation, but the class can be checked of conforms to protocol

1 Answers
Related