我正在创建一个锻炼跟踪应用程序来学习Swift和iOS开发。自从大学以来,我还没有写出15年以上的代码,所以我对这个MVC的东西很陌生。欢迎任何指导,但具体来说,我有一个关于如何在模型和控制器之间进行通信的最佳实践的问题。我应该从模型中返回类特定的类型到我的控制器吗?
我的模型(下面的类名“Exercise”)有它自己的类型(例如“Weights”,“Reps”),并且将这些类型返回到我的控制器来构建视图感觉不对。我的直觉是对的吗?我应该抽象这个更高级别的控制器,而不是特定于类的数据结构?或者,这是完全正常的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
}
}
和控制器:
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.
}
}
因此,将这些数据结构从Model传递到Controller符合MVC。这有帮助。 – briancl