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:
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]().
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.
Final Answer:
var dict = [String: Int]() -> Option C
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
Master "Swift Language Essentials" in iOS Swift
9 interactive learning modes - each teaches the same concept differently