[英]How to print an array of dictionary values into a nice string in Swift
我有一組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..."
。
您可以通過鏈接map
和joined
操作來構造單個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
的屬性routineName
和Routine
routineExercisesAndSets
是可選的。 如果是這種情況,您自然需要在訪問它們(可能存在的)值之前處理它們的展開。 例如,如果兩個屬性中的任何一個為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: ", ")
}
}
首先,讓Routine
符合CustomStringConvertible
extension Routine: CustomStringConvertible {
var description: String {
return routineExercisesAndSets.map { $0 + " x " + $1.description }.joined(separator: ", ")
}
}
現在您可以輕松編寫
routines.forEach { print($0) }
結果
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
或給定一個UITableViewCell
...
let cell: UITableViewCell = ...
let routine: Routine = ...
cell.detailTextLabel?.text = routine.description
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.