我想分配一个继承自其父类(BaseLectureViewController)的子类(TapWhatYouHearViewController) ) 使用继承类型 (TapWhatYouHear) 将泛型实现为父类 类型的变量 (BaseLectureViewController ) 与类型基类(游戏)的泛型。
Swift 产生以下错误:
Swift:: Error: cannot assign value of type 'TapWhatYouHearViewController' to type 'BaseLectureViewController?'
我读过,一个解决方案,例如在 Java 中会使用通配符泛型,但这在 Swift 中是不可能的。 还有其他解决办法吗?
这是我的设置:
/** 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> {
}
我想执行以下操作:
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()
我想做的是在运行时确定 BaseLectureViewController
的哪个子类应该分配给 viewController
变量。为了保存代码,我想将 Game
类型的游戏分配给 BaseLectureViewController
的 game
属性。
最佳答案
您想到的通用方法是不可能的。我的建议是使用一个简单的继承解决方案,您可以在具体的 ViewController(即 TapWhatYouHearViewController
)中覆盖 getGame()
并将游戏转换为您的特定类型。
import UIKit
enum GameType {
case TapWhatYouHear
case ChooseTranslation
}
class Game {
var title: String?
var type: GameType?
}
class TapWhatYouHear : Game {
var audio: String?
override init() {
super.init()
type = GameType.TapWhatYouHear
}
}
class ChooseTranslation : Game {
var video: String?
override init() {
super.init()
type = GameType.ChooseTranslation
}
}
/** VIEW CONTROLLERS **/
protocol Lecture {
var game: Game? { get set }
}
class BaseLectureViewController: UIViewController, Lecture {
var game: Game?
func getGame() -> Game? {
fatalError("Must be implemented in subclass")
}
}
class TapWhatYouHearViewController : BaseLectureViewController {
override func getGame() -> TapWhatYouHear? {
return game as? TapWhatYouHear
}
}
class ChooseTranslationViewController : BaseLectureViewController {
override func getGame() -> ChooseTranslation? {
return game as? ChooseTranslation
}
}
var game: Game = TapWhatYouHear() // <-- Depending on the user interaction, this can be any sub class of "Game"
var viewController: BaseLectureViewController?
if let gameType = game.type {
switch gameType {
case GameType.ChooseTranslation:
viewController = TapWhatYouHearViewController() // <--Swift:: Error: cannot assign value of type 'TapWhatYouHearViewController' to type 'BaseLectureViewController<Game>?'
case GameType.TapWhatYouHear:
viewController = ChooseTranslationViewController()
}
viewController?.game = TapWhatYouHear()
}
关于ios - Swift:将子类类型的对象分配给具有泛型的基类类型的变量会产生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56443089/