【发布时间】:2020-11-12 18:52:58
【问题描述】:
我刚刚完成了我的第一个应用,但工作被拒绝了。好消息是:我的代码的薄弱方面有 cmets。由于我缺乏经验,我什至很难找到修复它的方法。相关部分代码为:
Items.swift:
struct Items {
let imageName: String
let description: String
var location = (0, 0)
var collectedIndex = K.Values.notCollected
}
视图控制器:
var chest = Items(imageName: K.icons.chest, description: K.descriptions.chest)
var key = Items(imageName: K.icons.key, description: K.descriptions.key)
var rock = Items(imageName: K.icons.rock, description: K.descriptions.rock)
var bone = Items(imageName: K.icons.bone, description: K.descriptions.bone)
var mushroom = Items(imageName: K.icons.mushroom, description: K.descriptions.mushroom)
var apple = Items(imageName: K.icons.apple, description: K.descriptions.apple)
var items = [Items]()
ViewDidLoad:
items = [chest, key, rock, bone, mushroom, apple]
这有什么不好:
- “物品位置是作为物品的属性创建的。但也可以拾取或使用物品。对于这些操作,您不需要物品的位置。因此,此数据过多,仅在特定情况下使用。同样如此用于collectedIndex。这些属性应该位于另一个级别"
- “预先在 ViewController 中创建并通过 ViewController 的 vars 访问的固定数量的项目是非常糟糕的决定,这与良好编程的所有原则不符”。
在我的代码中,我确实需要为每个项目分配位置和collectedIndex,以便一切正常工作。
非常感谢任何帮助!
【问题讨论】:
-
您已经有了答案,但也许codereview.stackexchange 是您问题的更好目标受众
标签: swift xcode oop model-view-controller struct