gpt4 book ai didi

ios - 如何在 Swift 中将字典值数组打印成漂亮的字符串

转载 作者:行者123 更新时间:2023-11-28 10:16:30 27 4
gpt4 key购买 nike

我有一组 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 数组作为详细文本,并在使用 array.description 函数时在 tableView 中得到此结果

this result in the tableView

我的问题是,我如何遍历例程对象并打印出看起来像这样的“漂亮”格式化字符串:

“小腿抬高 x 4,深蹲 x 4,弓步 x 4...”

我可以使用以下代码在新行上打印出单独的键值对:

for i in routines{
for (key, value) in (i.routineExercisesAndSets)! {
print("\(key) x \(String(value))")
}
}

产生:

小腿抬高 x 4
深蹲 x 4
弓步 x 4
卧推 x 4
俯卧撑 x 4
苍蝇 x 4
上斜卧推 x 4
三头肌拉伸(stretch) x 4
三头肌浸入 x 4
传教士卷发 x 4
锤式弯举 x 4
二头肌 curl x 4

但我想按它们的父对象对它们进行分组,这样例程的练习就会显示在同一行上,因此它看起来像示例 “Calf Raises x 4, Squats x 4, Lunges x 4...”

最佳答案

您可以使用链式 mapjoined 操作来构造单个 String 来描述给定练习的练习字典。例如

for routine in routines {
print("Routine:", routine.routineName)
print("--------------")
print(routine.routineExercisesAndSets
.map { $0 + " x \($1)" }
.joined(separator: ", "))
print()
}
/* Routine: Legs
--------------
Calf Raises x 4, Squats x 4, Lunges x 4

Routine: Chest
--------------
Bench Press x 4, Press Ups x 4, Flyes x 4, Inclined Bench Press x 4

Routine: Arms
--------------
Tricep Extension x 4, Tricep Dips x 4, Preacher Curls x 4, Hammer Curls x 4, Bicep Curls x 4

*/

此外,您可以将它们实现为 Routine 的计算 description 属性的返回,而不是在每次使用时显式执行这些“表示”转换,作为一部分让 Routine 符合 CustomStringConvertible :

extension Routine: CustomStringConvertible {
var description: String {
return routineName + ":\n" + routineExercisesAndSets
.map { $0 + " x \($1)" }
.joined(separator: ", ")
}
}

for routine in routines {
print(routine) /* uses Routine:s implementation of 'CustomStringConvertible',
the computed property 'description`, specifically */
print()
}
/* Legs:
Calf Raises x 4, Squats x 4, Lunges x 4

Chest:
Bench Press x 4, Press Ups x 4, Flyes x 4, Inclined Bench Press x 4

Arms:
Tricep Extension x 4, Tricep Dips x 4, Preacher Curls x 4, Hammer Curls x 4, Bicep Curls x 4

*/

鉴于您在下面的评论,似乎 Routine 的属性 routineNameroutineExercisesAndSets 是可选的。如果是这种情况,您自然需要在访问它们的(可能存在的)值之前处理它们的解包。例如,如果两个属性中的任何一个为 nil,则返回一个空的 description,否则返回组合的例程名称和详细描述(如上面非可选示例中所做的) :

struct Routine {
let routineName: String?
let routineExercisesAndSets: [String: Int]?
}

extension Routine: CustomStringConvertible {
var description: String {
guard let name = routineName, let details = routineExercisesAndSets else { return "" }
return name + ":\n" + details.map { $0 + " x \($1)" }.joined(separator: ", ")
}
}

关于ios - 如何在 Swift 中将字典值数组打印成漂亮的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40692889/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com