首页 > 解决方案 > 迭代嵌套结构并返回每个结构的总值

问题描述

我正在试验 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)] 

标签: swiftstruct

解决方案


我认为这可能是您正在寻找的。

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” nameFighter

[fOne.name: totalOne, fTwo.name: totalTwo]

推荐阅读