Bird
0
0

How do you correctly initialize a dictionary as a value type in Swift?

easy📝 Syntax Q3 of 15
Swift - Collections
How do you correctly initialize a dictionary as a value type in Swift?
Avar dict = {"one": 1, "two": 2}
Blet dict = Dictionary<String, Int> = ("one", 1), ("two", 2)
Cvar dict: [String: Int] = ["one": 1, "two": 2]
Dvar dict = ["one" -> 1, "two" -> 2]
Step-by-Step Solution
Solution:
  1. Step 1: Recognize dictionary syntax

    Swift dictionaries use square brackets with key-value pairs separated by colons.
  2. Step 2: Validate options

    var dict: [String: Int] = ["one": 1, "two": 2] correctly declares a variable dictionary with string keys and integer values.
  3. Final Answer:

    var dict: [String: Int] = ["one": 1, "two": 2] -> Option C
  4. Quick Check:

    Dictionary syntax uses colons inside brackets [OK]
Quick Trick: Use [Key: Value] syntax for dictionaries [OK]
Common Mistakes:
  • Using parentheses instead of brackets
  • Using arrows (->) instead of colons
  • Incorrect assignment with let and type

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes