Swift - CollectionsHow 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]Check Answer
Step-by-Step SolutionSolution:Step 1: Recognize dictionary syntaxSwift dictionaries use square brackets with key-value pairs separated by colons.Step 2: Validate optionsvar dict: [String: Int] = ["one": 1, "two": 2] correctly declares a variable dictionary with string keys and integer values.Final Answer:var dict: [String: Int] = ["one": 1, "two": 2] -> Option CQuick Check:Dictionary syntax uses colons inside brackets [OK]Quick Trick: Use [Key: Value] syntax for dictionaries [OK]Common Mistakes:Using parentheses instead of bracketsUsing arrows (->) instead of colonsIncorrect assignment with let and type
Master "Collections" in Swift9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Swift Quizzes Control Flow - Why Swift has no implicit fallthrough - Quiz 13medium Data Types - Tuples for grouped values - Quiz 1easy Functions - Nested functions - Quiz 2easy Loops - Repeat-while loop - Quiz 4medium Loops - For-in loop with collections - Quiz 11easy Loops - Why Swift loops are safe by default - Quiz 12easy Loops - Break and continue behavior - Quiz 10hard Operators and Expressions - Logical operators - Quiz 10hard Optionals - Why optionals are Swift's core safety feature - Quiz 6medium Variables and Constants - Explicit type annotation - Quiz 12easy