【问题标题】:Two different model types in same view model? Swift - Xcode?同一视图模型中有两种不同的模型类型?斯威夫特 - Xcode?
【发布时间】:2020-02-10 21:47:42
【问题描述】:

我正在开发一个使用 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()
    }
}

。我无法更改此协议。

请帮忙?

【问题讨论】:

    标签: ios swift xcode mvvm graphql


    【解决方案1】:

    在 Swift 中,您需要在退出初始化程序之前为所有属性赋值。在这两种情况下,您似乎都没有为modelmodel1 赋值。如果它是有意在我们的另一个上使用,但不是同时使用,我会推荐一个具有关联值的枚举。像这样的:

    enum ModelObject {
        case model(Model)
        case model1(Model1)
    }
    

    然后在你的初始化器中你可以这样做:

    self.modelObject = .model1(model1)
    

    self.modelObject = .model(model)
    

    如果由于某种原因不起作用,您还可以将它们都设为可选并将一个分配给nil,另一个分配给它初始化的值。

    【讨论】:

    • 谢谢,很遗憾,viewModel 的协议不允许这样做?我无法更改此协议。这是否意味着不可能?请参阅编辑。
    • @Dan.code 首先,Xcode 会告诉你你缺少什么。只需单击带有正方形的红色小指示器。在这种情况下,它甚至可能会自动完成您需要的内容。其次,这是因为你没有在你的ProductViewModel 中定义associatedtype ModelType: Serializable
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 2020-08-07
    相关资源
    最近更新 更多