Bird
0
0

Which of the following is the correct way to create a dictionary in Swift?

easy📝 Syntax Q12 of 15
Swift - Collections
Which of the following is the correct way to create a dictionary in Swift?
Alet dict = ("apple", 1), ("banana", 2)
Blet dict = ["apple": 1, "banana": 2]
Clet dict = {"apple": 1, "banana": 2}
Dlet dict = ["apple", 1, "banana", 2]
Step-by-Step Solution
Solution:
  1. Step 1: Identify Swift dictionary syntax

    Swift dictionaries use square brackets with key-value pairs separated by colons.
  2. Step 2: Check each option

    let dict = ["apple": 1, "banana": 2] uses correct syntax: ["key": value, ...]. Others use tuples, braces, or arrays incorrectly.
  3. Final Answer:

    let dict = ["apple": 1, "banana": 2] -> Option B
  4. Quick Check:

    Dictionary syntax = [key: value] [OK]
Quick Trick: Use square brackets with colons for key-value pairs [OK]
Common Mistakes:
  • Using parentheses instead of square brackets
  • Using curly braces which define closures
  • Listing keys and values without colons

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes