ios - 同一视图模型中有两种不同的模型类型?斯威夫特 - Xcode?
问题描述
我正在开发一个使用 GraphQL 从 shopify 商店获取产品信息的项目。我通过搜索产品 ProductID 来查询数据,并以 Storefront.Product 的形式获取数据。但是,我的视图模型要求它采用 Storefront.ProductEdge 的形式。
我试图在同一个视图模型(ProductViewModel)中添加两种不同的模型类型(Storefront.Product 和 Storefront.ProductEdge)。
这是我的代码:
import Foundation
import Buy
final class ProductViewModel: ViewModel {
typealias ModelType = Storefront.ProductEdge
typealias ModelType1 = Storefront.Product
let model: ModelType
let model1: ModelType1
let cursor: String
var id: String
var title: String
var summary: String
var price: String
var images: PageableArray<ImageViewModel>
var variants: PageableArray<VariantViewModel> ///Ive changed let to var here so i can assign values in HomeController
// ----------------------------------
// MARK: - Init -
//
required init(from model: ModelType) {
self.model = model
self.cursor = model.cursor
let variants = model.node.variants.edges.viewModels.sorted {
$0.price < $1.price
}
let lowestPrice = variants.first?.price
self.id = model.node.id.rawValue
self.title = model.node.title
self.summary = model.node.descriptionHtml
self.price = lowestPrice == nil ? "No price" : Currency.stringFrom(lowestPrice!)
self.images = PageableArray(
with: model.node.images.edges,
pageInfo: model.node.images.pageInfo
)
self.variants = PageableArray(
with: model.node.variants.edges,
pageInfo: model.node.variants.pageInfo
)
}
required init(from model1: ModelType1){
self.model1 = model1
self.cursor = ""
let variants = model1.variants.edges.viewModels.sorted {
$0.price < $1.price
}
let lowestPrice = model1.variants.edges.first?.viewModel.price
self.id = model1.id.rawValue
self.title = model1.title
self.summary = model1.descriptionHtml
self.price = lowestPrice == nil ? "No price" : Currency.stringFrom(lowestPrice!)
self.images = PageableArray(
with: model1.images.edges,
pageInfo: model1.images.pageInfo
)
self.variants = PageableArray(
with: model1.variants.edges,
pageInfo: model1.variants.pageInfo
)
}
}
extension Storefront.ProductEdge: ViewModeling {
typealias ViewModelType = ProductViewModel
}
它在说
Return from initializer without initializing all stored properties
这可能吗?
请帮忙?
编辑:
通过制作模型和模型1选项或在模型或模型1上使用枚举,我得到了错误
Type 'ProductViewModel' does not conform to protocol 'ViewModel'
我是否需要以某种方式在 Storefront.ProductEdge 和 Storefront.Product 上执行 If 语句或枚举?
这是 ViewModel 协议:
import Foundation
protocol ViewModel: Serializable {
associatedtype ModelType: Serializable
var model: ModelType { get }
init(from model: ModelType)
}
extension ViewModel {
static func deserialize(from representation: SerializedRepresentation) -> Self? {
if let model = ModelType.deserialize(from: representation) {
return Self.init(from: model)
}
return nil
}
func serialize() -> SerializedRepresentation {
return self.model.serialize()
}
}
. 我无法更改此协议。
请帮忙?
解决方案
在 Swift 中,您需要在退出初始化程序之前为所有属性赋值。model
看来,在这两种情况下,您都没有为或赋值model1
。如果它是有意在我们的另一个上使用,但不是同时使用,我会推荐一个具有关联值的枚举。像这样的东西:
enum ModelObject {
case model(Model)
case model1(Model1)
}
然后在您的初始化程序中,您可以执行以下操作:
self.modelObject = .model1(model1)
或者
self.modelObject = .model(model)
如果由于某种原因不起作用,您还可以将它们都设为可选,并将一个分配给nil
初始化它的值,另一个分配给它。
推荐阅读
- c# - C# linq:使用查询语法在哪里插入 groupby
- javascript - 如何将地图上的绘制点保存到地理服务器?
- python - Python - 在 SQL 查询中获取关键字之后的项目
- angular - 当我传递要转换为高图表中不同时区的日期时,图表没有绘制?
- azure - 在 Azure Kubernetes 服务中启用 GSMA
- excel - 如何在 vba 中动态更改图形的选择范围
- c# - C# 单元测试中未加载 C++ DLL
- rust - 过滤器内的变异向量
- amazon-web-services - AWS GreenGrass 连接问题
- amazon-web-services - Alexa Smart Home Skill - 使用 Lambda 转发事件