let animal["legs"] = currentAnimal.legCount
let animal["canFly"] = currentAnimal.canFly
let animal["chicken"] = currentAnimal.isSoonToBeNugget
let anim....
其中一个是nil
。我有大约10个这些并且想要将每个值打印到控制台而不必去
print(animal["legs"])
print(animal["canFly"])
print(animal["chicken"])
print(anim....)
最简洁的方法是什么?
答案 0 :(得分:5)
只需通过字典迭代并打印键值对:
for (key, value) in animal {
print("\(key) : \(value)")
}