gpt4 book ai didi

ios - 我应该将特定于类的类型从我的模型返回到我的 Controller 吗?

转载 作者:行者123 更新时间:2023-11-28 06:44:42 26 4
gpt4 key购买 nike

我正在创建一个锻炼跟踪应用程序来学习 Swift 和 iOS 开发。自从大学毕业以来,我已经 15 年多没有写过代码了,所以我对 MVC 这个东西还是陌生的。欢迎任何指导,但具体来说,我有一个关于如何在模型和 Controller 之间进行通信的最佳实践的问题。

我的模型(下面的类名“Exercise”)有自己的类型(例如,“Weights”、“Reps”),将这些类型返回到我的 Controller 以构建 View 感觉不对。我的直觉是对的吗?我是否应该将这一层抽象得更高,以便 Controller 请求值而不是特定于类的数据结构?或者这对于 MVC 来说完全正常吗?

下面是一些代码来演示我现在在做什么,感觉不对。有问题的错误部分在 configureView() 中。

模型:

class Exercise {

var name = "placeholder name"
var formNotes = "placeholder notes"
private var repLog: [repLogEntry] = []
private var currentWeights = Weights(warmup25: 0, warmup50: 0, heavy: 0)

init() {
// some test data
name = "Squat"
formNotes = "Grasp the bar 1 inch outside knurling\nPull bar apart while squeezing shoulders back"
self.recordExercise("16-04-20", weight: 135, reps: Reps(firstSet: 10, secondSet: 8))
self.recordExercise("16-04-22", weight: 135, reps: Reps(firstSet: 11, secondSet: 9))
self.setWeights(135)


}

// data to be recorded
private struct repLogEntry {
var date: String
var weight: Int
var reps: Reps

}

// reps always come in pairs
struct Reps {
var firstSet: Int
var secondSet: Int
}

// weights of the exercise
struct Weights {
var warmup25: Int
var warmup50: Int
var heavy: Int
}

func recordExercise(date: String, weight: Int, reps: Reps) {
let newRepLogEntry = repLogEntry(date: date, weight: weight, reps: reps)
repLog.append(newRepLogEntry)
}

func setWeights(newWeight: Int) {
currentWeights.heavy = newWeight
currentWeights.warmup25 = Int(Double(newWeight) * 0.25)
currentWeights.warmup50 = Int(Double(newWeight) * 0.50)
}

func getCurrentWeights() -> Weights {
return currentWeights
}

func getLastWorkoutReps() -> Reps {
return repLog.last!.reps
}

func getLastWorkoutDate() -> String {
return repLog.last!.date
}

func getLastWorkoutWeight() -> Int {
return repLog.last!.weight
}
}

和 Controller :

class DetailViewController: UIViewController {

@IBOutlet weak var detailDescriptionLabel: UILabel!

@IBOutlet weak var label: UILabel?

let Squat = Exercise()

var detailItem: AnyObject? {
didSet {
// Update the view.
self.configureView()
}
}

func configureView() {
// Update the user interface for the detail item.
if let detail = self.detailItem {
if let label = self.detailDescriptionLabel {
label.text = detail.valueForKey("timeStamp")!.description
}
}

let currentWeights = Squat.getCurrentWeights()
let exerciseName = Squat.name
let formNotes = Squat.formNotes
let warmup25Text = String(currentWeights.warmup25)
let warmup50Text = String(currentWeights.warmup50)
let heavyText = String(currentWeights.heavy)
let lastWorkoutReps = Squat.getLastWorkoutReps()
let lastWorkoutDate = Squat.getLastWorkoutDate()
let lastWorkoutWeight = Squat.getLastWorkoutWeight()

label?.text = "\(exerciseName)\nWarmup (25%): \(warmup25Text)\nWarmup (50%): \(warmup50Text)\nHeavy (100%): \(heavyText)\n\(lastWorkoutDate) Reps @\(lastWorkoutWeight): \(lastWorkoutReps.firstSet) and \(lastWorkoutReps.secondSet)\n\nForm Notes:\n\(formNotes)"

}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.configureView()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

最佳答案

对于 MVC,您所做的是“好的”。您将模型、 View 和 Controller 分开, Controller 在模型和 View 之间进行调解。

在 MVC 中, Controller 通常管理 View 上的所有数据布局和 View 上的所有用户交互,因此它可能变得庞大而困惑。有时您会将模型传递给 View 并让 View 自行配置,这会使 Controller 更简单,但不是好的 MVC。

您可以考虑使用 MVVM - Model View ViewModel - 它是 MVC 的扩展。基本上,它采用如何从 Controller 中显示模型数据的逻辑,并将其放入 ViewModel,ViewModel 提供给 View 以供其配置自身。这简化了 Controller ,但也将 View 与模型隔离开来。

因此,您的 ViewModel 会有您的 text = "\(exerciseName... 代码,但仅此而已。它只知道如何获取模型对象并生成应该显示的文本。它不知道它要显示在哪里。View 配置了这个 ViewModel,所以它可以获取它需要的数据,但它不知道它来自哪里。Controller 是创建 ViewModel 并将其传递给的中间人 View 。

关于ios - 我应该将特定于类的类型从我的模型返回到我的 Controller 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36842378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com