Swift - CollectionsWhat is the issue with this Swift code snippet?var data = [] data.append(10)AThe append method is not available for arrays in Swift.BYou cannot append elements to a variable declared with 'var'.CThe array 'data' has no type specified, so Swift cannot infer the element type.DThe array must be declared with 'let' to allow appending.Check Answer
Step-by-Step SolutionSolution:Step 1: Analyze the declarationThe array 'data' is declared as an empty array with no type annotation, so Swift cannot infer its element type.Step 2: Understand type inference rulesSwift requires either explicit type annotation or initial elements to infer the array's type.Step 3: Identify the errorSince 'data' has no type, calling 'append(10)' causes a compile-time error.Final Answer:The array 'data' has no type specified, so Swift cannot infer the element type. -> Option CQuick Check:Empty array without type annotation cannot infer element type [OK]Quick Trick: Empty arrays need explicit type for appending [OK]Common Mistakes:Assuming 'var' prevents appendingThinking append is unavailable for arraysBelieving 'let' is required for appending
Master "Collections" in Swift9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Swift Quizzes Collections - Dictionary creation and access - Quiz 11easy Collections - Set algebra (union, intersection, difference) - Quiz 1easy Collections - Dictionary creation and access - Quiz 5medium Control Flow - No implicit fallthrough in switch - Quiz 8hard Data Types - Type conversion is always explicit - Quiz 8hard Data Types - Type conversion is always explicit - Quiz 10hard Loops - Why Swift loops are safe by default - Quiz 8hard Loops - Repeat-while loop - Quiz 15hard Operators and Expressions - Operator overloading concept - Quiz 1easy Variables and Constants - Semicolons are optional behavior - Quiz 7medium