How to pass Child Class reference as Parent Class

So I have this child class with a function that creates a perdetermined array of other classes.

class DirectGame : GameParent {
    
    static func GetAllChallenges() -> Array<ChallengeParent>{
        return [LockdownChallenge(game: self)]
    }

}

These other classes take in a GameParent class in the initalizer like so:

class LockdownChallenge {
    
    var game : GameParent
    
    
    init(game: GameParent) {
        self.game = game
    }
}

However this line

return [LockdownChallenge(game: self)]

is throwing the error "Cannot convert value of type 'DirectGame.Type' to expected argument type 'GameParent'"

How do I pass in a reference to DirectGame into the initalizer of ChallengeParent?

You are conflating the ideas of instances and metatypes. Is that sentence enough for you to solve the problem or do you need to know where to find links with relevant information?

The func getAllChallenges declares to return [ChallengeParent].

but it returns an array of LockdownChallenge

That’s the cause of error.

As you don’t show how ChallengeParent relates to other classes, hard to tell what to change precisely. But you have to resolve the inconsistency. Either by changing the return declaration of the func or by changing what it returns.

How to pass Child Class reference as Parent Class
 
 
Q