繁体   English   中英

同一视图模型中有两种不同的模型类型? Swift - Xcode?

[英]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 中,您需要在退出初始化程序之前为所有属性赋值。 在这两种情况下,您似乎都没有为modelmodel1分配值。 如果有意在我们的另一个上使用,但不是两者都使用,我建议使用具有关联值的枚举。 像这样的东西:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM