Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 同一视图模型中有两种不同的模型类型?Swift-Xcode?_Ios_Swift_Xcode_Mvvm_Graphql - Fatal编程技术网

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

Ios 同一视图模型中有两种不同的模型类型?Swift-Xcode?,ios,swift,xcode,mvvm,graphql,Ios,Swift,Xcode,Mvvm,Graphql,我正在做一个项目,它使用GraphQL从shopify商店获取产品信息。 我通过搜索ProductID来查询数据,并以Storefront.Product的形式获取数据。 但是,我的视图模型要求其格式为Storefront.ProductEdge 我试图在同一视图模型(ProductViewModel)中添加两种不同的模型类型(Storefront.Product和Storefront.ProductEdge) 这是我的密码: import Foundation import Buy fina

我正在做一个项目,它使用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
}

这可能吗

请帮忙

编辑:

通过设置model和model1选项或在model或model1上使用枚举,我得到了错误

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中,您需要在退出初始值设定项之前为所有属性指定一个值。在这两种情况下,您可能没有为
模型
模型1
赋值。如果有意使用另一个,但不是两者都使用,我建议使用带有关联值的枚举。大概是这样的:

enum ModelObject {
    case model(Model)
    case model1(Model1)
}
然后在初始值设定项中,您可以执行以下操作:

self.modelObject = .model1(model1)


如果由于某种原因无法工作,您也可以将它们都设置为可选,并将一个指定给
nil
,将另一个指定给它初始化时使用的值。

谢谢,不幸的是,viewModel协议不允许这样做?我无法更改此协议。这是否意味着这是不可能的?请先查看edit.@Dan.code,Xcode将告诉您缺少什么。只需单击带有正方形的红色小指示器。在这种情况下,它甚至可能自动完成您需要的内容。其次,这是因为您没有在
ProductViewModel
中定义
associatedtype模型类型:Serializable
self.modelObject = .model1(model1)
self.modelObject = .model(model)