To print a dictionary in Swift, you can use a `for` loop to iterate over the key-value pairs and print each pair using the `print` function.
We can print a dictionary in Swift where the keys are integers and the values are strings. This example demonstrates how to iterate over a dictionary and print its contents.
For example,
myDictionary
with integer keys and string values.var myDictionary: [Int: String] = [
1: "One",
2: "Two",
3: "Three"
]
// Print the dictionary contents
print("Dictionary contents:")
for (key, value) in myDictionary {
print("Key: \(key), Value: \(value)")
}
Dictionary contents: Key: 1, Value: One Key: 2, Value: Two Key: 3, Value: Three
We can print a dictionary in Swift where the keys are strings and the values are doubles. This example demonstrates how to iterate over a dictionary and print its contents.
For example,
myDictionary
with string keys and double values.var myDictionary: [String: Double] = [
"A": 1.1,
"B": 2.2,
"C": 3.3
]
// Print the dictionary contents
print("Dictionary contents:")
for (key, value) in myDictionary {
print("Key: \(key), Value: \(value)")
}
Dictionary contents: Key: A, Value: 1.1 Key: B, Value: 2.2 Key: C, Value: 3.3
In this tutorial, we learned How to Print a Dictionary in Swift language with well detailed examples.