Swift - Basics and RuntimeWhy does Swift use optionals instead of allowing null or nil values directly in variables?ATo allow variables to change type dynamicallyBTo force developers to handle absence of values explicitly, reducing runtime errorsCTo make code shorter by ignoring null checksDBecause Swift does not support nil values at allCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand optionals in SwiftOptionals wrap a value that may be present or absent, requiring explicit handling.Step 2: Reason why this design reduces errorsThis prevents unexpected nil crashes by making absence visible in code.Final Answer:To force developers to handle absence of values explicitly, reducing runtime errors -> Option BQuick Check:Optionals improve safety = D [OK]Quick Trick: Optionals make nil explicit and safe to handle [OK]Common Mistakes:Thinking Swift disallows nil completelyAssuming optionals skip null checksBelieving optionals allow dynamic typing
Master "Basics and Runtime" in Swift9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Swift Quizzes Collections - Why collections are value types in Swift - Quiz 9hard Collections - Dictionary creation and access - Quiz 15hard Control Flow - No implicit fallthrough in switch - Quiz 10hard Control Flow - No implicit fallthrough in switch - Quiz 5medium Data Types - Character and String types - Quiz 15hard Data Types - Int, Double, Float number types - Quiz 6medium Functions - Why functions are first-class in Swift - Quiz 6medium Optionals - Multiple optional binding - Quiz 7medium Swift Basics and Runtime - Project structure and Swift Package Manager basics - Quiz 5medium Swift Basics and Runtime - Swift REPL and Playgrounds - Quiz 11easy