Swift - LoopsYou want to safely remove all even numbers from an array while looping. Which approach respects Swift's loop safety?AModify the array inside a while loop without checksBRemove even numbers directly inside a for-in loop over the arrayCUse a for-in loop and remove elements by index during iterationDCreate a new array filtering out even numbers, then assign it backCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand safe mutationModifying a collection during iteration is unsafe in Swift.Step 2: Use filtering to create a new arrayFiltering creates a new array without modifying the original during iteration.Final Answer:Create a new array filtering out even numbers, then assign it back -> Option DQuick Check:Safe removal = Use filtered copy [OK]Quick Trick: Filter to remove items safely, don't mutate in loop [OK]Common Mistakes:Removing items inside for-in loopModifying array by index during iterationIgnoring Swift's safety rules
Master "Loops" in Swift9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Swift Quizzes Collections - Array iteration and enumerated - Quiz 4medium Collections - Array iteration and enumerated - Quiz 14medium Control Flow - Switch must be exhaustive - Quiz 15hard Data Types - Type conversion is always explicit - Quiz 7medium Functions - Why functions are first-class in Swift - Quiz 11easy Operators and Expressions - Operator overloading concept - Quiz 3easy Swift Basics and Runtime - Comments and documentation markup - Quiz 13medium Swift Basics and Runtime - Swift REPL and Playgrounds - Quiz 12easy Swift Basics and Runtime - Swift REPL and Playgrounds - Quiz 7medium Variables and Constants - Type inference by the compiler - Quiz 1easy