[英]Two different model types in same view model? Swift - 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
,另一個分配給它初始化的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.