Bird
0
0

Which of the following is the correct way to declare an empty dictionary in Swift?

easy📝 Syntax Q12 of 15
iOS Swift - Swift Language Essentials
Which of the following is the correct way to declare an empty dictionary in Swift?
Avar dict = Set<String>()
Bvar dict = [Int]()
Cvar dict = [String: Int]()
Dvar dict = Array<Int>()
Step-by-Step Solution
Solution:
  1. Step 1: Identify dictionary syntax

    In Swift, dictionaries are declared with key and value types inside square brackets separated by a colon, e.g., [KeyType: ValueType]().
  2. Step 2: Check options

    var dict = [String: Int]() uses [String: Int]() which is correct for an empty dictionary with String keys and Int values. Others are array or set declarations.
  3. Final Answer:

    var dict = [String: Int]() -> Option C
  4. Quick Check:

    Dictionary syntax = [Key: Value]() [OK]
Quick Trick: Dictionaries use [Key: Value]() syntax [OK]
Common Mistakes:
  • Using [Int]() which is an array, not dictionary
  • Confusing sets with dictionaries
  • Missing colon between key and value types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes