gpt4 book ai didi

swift - 符合 associatedtype 的协议(protocol)

转载 作者:行者123 更新时间:2023-11-28 09:23:14 24 4
gpt4 key购买 nike

<分区>

我坚持使用 Swift 语法。此代码示例无法编译,我也找不到原因。它说

Type 'TextMessageViewModelDefaultBuilder' does not conform to protocol 'ViewModelBuilderProtocol'

import Foundation

protocol MessageModelProtocol { }
protocol MessageViewModelProtocol { }

protocol TextMessageViewModelProtocol:MessageViewModelProtocol {}
protocol TextMessageModelProtocol:MessageModelProtocol {}

protocol ViewModelBuilderProtocol {
associatedtype ModelT: MessageModelProtocol
associatedtype ViewModelT: MessageViewModelProtocol
func canCreateViewModel(fromModel model: Any) -> Bool
func createViewModel(_ model: ModelT) -> ViewModelT
}

class TextMessageViewModelDefaultBuilder: ViewModelBuilderProtocol {
typealias ModelT = TextMessageModelProtocol
typealias ViewModelT = TextMessageViewModelProtocol

init() {}

func createViewModel(_ textMessage: ModelT) -> ViewModelT {
return ViewModelT()
}

func canCreateViewModel(fromModel model: Any) -> Bool {
return model is ModelT
}
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com