【发布时间】:2016-11-19 12:29:22
【问题描述】:
我有一个 routine 对象数组,它们被定义为:
struct Routine {
let routineName: String
let routineExercisesAndSets: [String: Int]
}
let routines: [Routine] = {
let firstRoutine = Routine(routineName: "Legs", routineExercisesAndSets: ["Squats":4,"Lunges":4,"Calf Raises":4])
let secondRoutine = Routine(routineName: "Chest", routineExercisesAndSets: ["Bench Press":4,"Press Ups":4,"Flyes":4,"Inclined Bench Press":4])
let thirdRoutine = Routine(routineName: "Arms", routineExercisesAndSets: ["Bicep Curls":4,"Hammer Curls":4,"Preacher Curls":4,"Tricep Extension":4,"Tricep Dips":4])
return [firstRoutine, secondRoutine, thirdRoutine]
}()
我试图将它们 routineExercisesAndSets 数组作为详细文本返回到 UITableViewCell 中,当使用 array.description 函数时,我在 tableView 中得到了这个结果
我的问题是,我如何遍历例程对象并打印出类似于以下内容的“漂亮”格式字符串:
"Calf Raises x 4, Squats x 4, Lunges x 4..."
我可以使用以下代码在新行上打印出单独的键、值对:
for i in routines{
for (key, value) in (i.routineExercisesAndSets)! {
print("\(key) x \(String(value))")
}
}
产生:
Calf Raises x 4
Squats x 4
Lunges x 4
Bench Press x 4
Press Ups x 4
Flyes x 4
Inclined Bench Press x 4
Tricep Extension x 4
Tricep Dips x 4
Preacher Curls x 4
Hammer Curls x 4
Bicep Curls x 4
但我想按它们的父对象对它们进行分组,以便例程的练习显示在同一行,因此它看起来像示例 "Calf Raises x 4, Squats x 4, Lunges x 4..."。
【问题讨论】:
标签: ios arrays swift dictionary