Skip to content

Swift Practice (Basics)

Swift — Interactive Practice

10 auto-graded practice problems covering core Swift concepts. Select an answer, submit, and review the explanation.


Types and Variables


Optionals


Collections


Closures


OOP and Error Handling

Intuition

Swift combines safety with expressiveness: Type inference, optionals, and value types (structs) catch errors at compile time while keeping code concise. Protocol-oriented design provides flexibility without inheritance complexity.

Why it matters: Swift’s safety features prevent common bugs, while its performance makes it suitable for demanding applications.

The key insight: Swift structs are value types (copied on assignment) while classes are reference types (shared), choosing the right type depends on whether you want copy semantics or shared identity.

Common Mistakes

Confusing value types and reference types: Swift structs and enums are value types (copied on assignment). Classes are reference types (shared reference). Modifying a copy of a struct does not affect the original. Modifying a class instance affects all references to it.

Forgetting that String is a value type: Strings in Swift are value types. When you assign let b = a, b is a copy. Mutating a does not affect b. This is different from NSString (reference type) in Objective-C.

Overusing force unwrapping (!): Force unwrapping crashes if the optional is nil. Use optional binding, nil coalescing, or optional chaining instead. Reserve ! for cases where you are absolutely certain the value is non-nil.

See Also

Detailed Content

Core Concepts

Worked Examples

Common Mistakes

  • Rushing through foundational material
  • Not practising problems after reading
  • Failing to connect concepts across topics

Further Reading