Bird
0
0

Find the error in this Swift code snippet:

medium📝 Debug Q7 of 15
Swift - Data Types
Find the error in this Swift code snippet:
var text = "Swift"
text[0] = "s"
AString must be declared with let, not var
BCannot assign lowercase letter to uppercase string
CStrings cannot be indexed by integer subscripts
DMissing import statement for String manipulation
Step-by-Step Solution
Solution:
  1. Step 1: Understand Swift String indexing

    Swift strings do not support direct integer indexing like arrays; you must use String.Index.
  2. Step 2: Identify the error cause

    Using text[0] causes a compile error because integer subscripts are not allowed.
  3. Final Answer:

    Strings cannot be indexed by integer subscripts -> Option C
  4. Quick Check:

    Use String.Index, not integer, to access characters [OK]
Quick Trick: Use String.Index, not integer, to access characters [OK]
Common Mistakes:
  • Trying to access string characters with integer index
  • Assuming strings behave like arrays
  • Ignoring Swift's String.Index type

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes