How to check if Data Type equals Generic Data Type and assign to Property if True?

Context

I have a Generic Swift Class containing a component Property of the Generic Type. A few other Variables with different Data Types are passed through the Initializer, however, all of them are conforming to the Generic Protocol.

However, I get the following Compiler Error in Line 11:

'ComponentA' is not convertible to 'C'


Code

protocol Component { ... }

struct ComponentA: Component { ... }
struct ComponentB: Component { ... }

class Main<C: Component> {
    var component: C

    init(componentA: ComponentA, componentB: ComponentB) {
        // I am trying to check, whether componentA equals the Generic Data Type and assign it to the component Property if true.
        if case let safeComponent = componentA as C {
            self.component = safeComponent
        }
    }
}

Question

How can I achieve my goal of checking whether a Data Type equals the Generic Data Type and assign it to the component Property if true?

Answered by ChristophJR in 725513022

Found an Answer:


Code

protocol Component { ... }

struct ComponentA: Component { ... }
struct ComponentB: Component { ... }

class Main<C: Component> {
    var component: C

    init(componentA: ComponentA, componentB: ComponentB) {
        // I am trying to check, whether componentA equals the Generic Data Type and assign it to the component Property if true.
        if  let safeComponent = componentA as? C {
            self.component = safeComponent
        }
    }
}
Accepted Answer

Found an Answer:


Code

protocol Component { ... }

struct ComponentA: Component { ... }
struct ComponentB: Component { ... }

class Main<C: Component> {
    var component: C

    init(componentA: ComponentA, componentB: ComponentB) {
        // I am trying to check, whether componentA equals the Generic Data Type and assign it to the component Property if true.
        if  let safeComponent = componentA as? C {
            self.component = safeComponent
        }
    }
}
How to check if Data Type equals Generic Data Type and assign to Property if True?
 
 
Q