【问题标题】:Swift MVVM: Where to keep custom model data if we have an array of modelsSwift MVVM:如果我们有一组模型,在哪里保存自定义模型数据
【发布时间】:2019-11-15 04:05:19
【问题描述】:

我有一个json响应和模型如下

[
  {
  "name":"Name 1",
  "class":"class 1",
  "school":"school 1"
  },
  {
  "name":"Name 2",
  "class":"class 2",
  "school":"school 3"
  }
]

及其模型类如下

typealias StudentArray = [Student]

class Student: Codable {
    let name: String
    let section: String
    let school: String

    init(name: String, section: String, school: String) {
        self.name = name
        self.section = section
        self.school = school
    }
}

我想根据部分过滤整个数组,并为每个部分保留单独的数组。

我还需要保留其他过滤后的数据。

哪个地方最适合它? ViewModel 或 Student Model 类。

【问题讨论】:

  • ViewModel 是我推荐的地方。
  • 业务逻辑应该在 Model 中,Presentation 逻辑应该在 ViewModel 中是我知道的合适的方式。
  • Student 是一个对象,它不知道一组 Student [Student],所以肯定在 ViewModel 中。

标签: ios swift mvvm architecture


【解决方案1】:

模型方面。

模型是您的实际数据。 Viewer 观察 ViewModel 中的数据,ViewModel 解析 Model 中的数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 2020-05-11
    • 2012-12-12
    • 2014-02-11
    相关资源
    最近更新 更多