title: How to print an array of dictionary values into a nice string in Swift
body: <p>I have an array of <code>routine</code> objects thats are defined as:</p>
  
  <pre><code>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]
  }()
  </code></pre>
  
  <p>I am trying to return them <code>routineExercisesAndSets</code> array into a <code>UITableViewCell</code> as detail text and when using the array.description function I get this result in the tableView</p>
  
  <p><a href="https://i.stack.imgur.com/3KWa6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3KWa6.png" alt="this result in the tableView"></a></p>
  
  <p>My question is, how could I iterate through the routine objects and print out a 'pretty' formatted string that looks something like: </p>
  
  <p><code>"Calf Raises x 4, Squats x 4, Lunges x 4..."</code></p>
  
  <p>I can print out the individual key, value pairs on new lines using this code:</p>
  
  <pre><code>for i in routines{
      for (key, value) in (i.routineExercisesAndSets)! {
          print("\(key) x \(String(value))")
      }
  }
  </code></pre>
  
  <p>which produces:</p>
  
  <p><code>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</code></p>
  
  <p>but I want to group them by their parent object so the exercises for a routine display on the same line so it looks like the example <code>"Calf Raises x 4, Squats x 4, Lunges x 4..."</code>.</p>