swift - 迭代嵌套结构并返回每个结构的总值
问题描述
我正在试验 Swift,试图创建一个基本的游戏引擎。
我有一个Struct
定义战斗机的,其中有许多结构用于定义战斗机所拥有的技能。
我想遍历每个“战士”并汇总每个单独的技能集,基本上返回 3 个值 - 总计standUp, clinch, ground
我可以遍历Fighter
结构,但是我只能注销那些嵌套结构。
我应该如何处理循环遍历结构然后循环遍历嵌套结构,将每个循环的总和返回为它自己的值?也许作为元组的一部分?
import UIKit
struct StandUp {
let boxing: Int
let kickBoxing: Int
}
struct Clinch {
let judo: Int
let freestyle: Int
}
struct Ground {
let bjj: Int
let judo: Int
}
struct Fighter {
let standUp: StandUp
let clinch: Clinch
let ground: Ground
}
let striker = Fighter(
standUp: StandUp(boxing: 8, kickBoxing: 7),
clinch: Clinch(judo: 5, freestyle: 4),
ground: Ground(bjj: 6, judo: 5)
)
let bjj = Fighter(
standUp: StandUp(boxing: 5, kickBoxing: 4),
clinch: Clinch(judo: 7, freestyle: 8),
ground: Ground(bjj: 8, judo: 7)
)
class FightEngine {
private let fOne: Fighter
private let fTwo: Fighter
init(fighterOne: Fighter, fighterTwo: Fighter) {
fOne = fighterOne
fTwo = fighterTwo
}
private func sumSkillSet(fighter: Fighter) -> Int {
var total: Int
let mirror = Mirror(reflecting: fighter)
for skill in mirror.children {
print(skill.value)
}
return 1 // Only returning this so the code will run
}
func getTotals() -> (Int, Int) {
let totalOne = sumSkillSet(fighter: fOne)
let totalTwo = sumSkillSet(fighter: fTwo)
return (1, 2) // Only returning this so the code will run
}
}
let fE = FightEngine(fighterOne: striker, fighterTwo: bjj)
fE.getTotals() // This should be a dictionary of each fighters totals, 3 values for each fighter
fE.getTotals()
理想情况下,我希望得到某种回报
["fighterOne":(1,2,3), "fighterTwo":(4,5,6)]
解决方案
我认为这可能是您正在寻找的。
struct StandUp {
let boxing: Int
let kickBoxing: Int
func sum() -> Int {
return boxing + kickBoxing
}
}
struct Clinch {
let judo: Int
let freestyle: Int
func sum() -> Int {
return judo + freestyle
}
}
struct Ground {
let bjj: Int
let judo: Int
func sum() -> Int {
return bjj + judo
}
}
struct Fighter {
let standUp: StandUp
let clinch: Clinch
let ground: Ground
func sum() -> [Int] {
return [standUp.sum(), clinch.sum(), ground.sum()]
}
}
let striker = Fighter(
standUp: StandUp(boxing: 8, kickBoxing: 7),
clinch: Clinch(judo: 5, freestyle: 4),
ground: Ground(bjj: 6, judo: 5)
)
let bjj = Fighter(
standUp: StandUp(boxing: 5, kickBoxing: 4),
clinch: Clinch(judo: 7, freestyle: 8),
ground: Ground(bjj: 8, judo: 7)
)
class FightEngine {
private let fOne: Fighter
private let fTwo: Fighter
init(fighterOne: Fighter, fighterTwo: Fighter) {
fOne = fighterOne
fTwo = fighterTwo
}
func getTotals() -> [String: [Int]] {
return ["fighterOne": fOne.sum(), "fighterTwo": fTwo.sum()]
}
}
注意:我建议您添加一个参数并将其用作键而不是“fighterOne”和“fighterTwo” name
。Fighter
[fOne.name: totalOne, fTwo.name: totalTwo]
推荐阅读
- json - 如何使用Vue axios无限滚动从json获取数据列表
- java - 是否有可能丢失数据或 Ignite Cache 中的不同键上存在不同的数据?
- java - 无法将文件放入 Java 字节数组
- c - 通过c将密码输入终端
- python - django中的cookiecutter错误
- firebase - Firebase:通过云功能检查用户电子邮件是否经过验证
- python - 如何创建一个快速的 Python GUI 来控制用于面部检测的网络摄像头输入?
- amazon-web-services - 如何正确(可扩展地)将许多 ORC 文件读入 spark
- python - 我无法将其他组的用户添加到我的组
- adb - AndroidVIewClient-如何在多台设备上同时运行一个python脚本