2

Say you're decoding a huge JSON file:

struct Examp: Codable {
  let Cats: [CatsItem]
  let Dogs: [DogsItem]
  let Horses: [HorsesItem]
  .. forty more
}

After you parse it, on the console you simply want to see how many of each:

print("cats .. \(result.Cats.count)")
print("dogs .. \(result.Dogs.count)")

and so on, the result being cats 1,015, dogs 932, etc.

I'm too lazy to repeatedly type a line of code like this:

print("cats .. \(result.Cats.count)")

Without having to modify Examp in any way, is there a way to do something like:

for (thing) in Examp { print("\(thing string) .. \(result.(thing).count)") }
0

1 Answer 1

1
+100

You can use a Mirror to reflect on the Examp, and loop through its children. You can then print the name and value of each child. To get the array's size, you can cast the value to [Any], since arrays are covariant (any type of array can be casted to [Any]).

For example:

struct Example: Codable {
    let cats: [String]
    let dogs: [String]
    let horses: [String]
}

let example = Example(cats: [""], dogs: ["", ""], horses: ["", "", ""])
let mirror = Mirror(reflecting: example)
for (name, value) in mirror.children {
    guard let name, let array = value as? [Any] else { continue }
    print("\(name) .. \(array.count)")
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.