I want to assign a sub class (TapWhatYouHearViewController), that inherits from its parent class (BaseLectureViewController) with implementing the generic using an inherited type (TapWhatYouHear), to a variable of type parent class (BaseLectureViewController) with the generic of type base class (Game).
Swift procudes the following error:
Swift:: Error: cannot assign value of type 'TapWhatYouHearViewController' to type 'BaseLectureViewController?'
I've read, that one solution e.g. in Java would be using a wildcard generic, but this is not possible in Swift. Is there any other solution?
This is my setup:
/** GAME TYPES **/
enum GameType {
case TapWhatYouHear,
case ChooseTranslation
}
class Game {
var title: String?
var type: GameType?
}
class TapWhatYouHear : Game {
var audio: String?
var type: GameType = GameType.TapWhatYouHear
}
class ChooseTranslation : Game {
var video: String?
var type: GameType = GameType.ChooseTranslation
}
/** VIEW CONTROLLERS **/
class BaseLectureViewController<T: Game> {
var game: T?
}
class TapWhatYouHearViewController : BaseLectureViewController<TapWhatYouHear> {
}
class ChooseTranslationViewController : BaseLectureViewController<ChooseTranslation> {
}
I want to do the following:
var game: Game = TapWhatYouHear() // <-- Depending on the user interaction, this can be any sub class of "Game"
var viewController: BaseLectureViewController<Game>
switch game.type {
case .ChooseTranslation:
viewController = TapWhatYouHearViewController() // <--Swift:: Error: cannot assign value of type 'TapWhatYouHearViewController' to type 'BaseLectureViewController<Game>?'
case .TapWhatYouHear:
viewController = ChooseTranslationViewController()
}
viewController.game = TapWhatYouHear()
What I'm trying to do is determine at runtime which sub class of BaseLectureViewController should be assigned to the viewController variable. To save code, I then want to assign a game of type Game to the game property of the BaseLectureViewController.