Swift - CollectionsWhich 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]Check Answer
Step-by-Step SolutionSolution:Step 1: Identify Swift dictionary syntaxSwift dictionaries use square brackets with key-value pairs separated by colons.Step 2: Check each optionlet dict = ["apple": 1, "banana": 2] uses correct syntax: ["key": value, ...]. Others use tuples, braces, or arrays incorrectly.Final Answer:let dict = ["apple": 1, "banana": 2] -> Option BQuick 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 bracketsUsing curly braces which define closuresListing keys and values without colons
Master "Collections" in Swift9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Swift Quizzes Collections - Array operations (append, insert, remove) - Quiz 8hard Collections - Set algebra (union, intersection, difference) - Quiz 2easy Data Types - Character and String types - Quiz 9hard Functions - Function declaration syntax - Quiz 12easy Loops - For-in loop with collections - Quiz 5medium Loops - Why Swift loops are safe by default - Quiz 4medium Operators and Expressions - Comparison operators - Quiz 6medium Operators and Expressions - Logical operators - Quiz 11easy Optionals - Guard let for early exit - Quiz 4medium Variables and Constants - Type inference by the compiler - Quiz 5medium