Swift BasicsLesson 18Free preview
Swift For Loop
This lesson is part of Swift · All levels
Summary
This lesson reviews the 'for-in' repetition in Swift that is used to iterate the sequence of a number array, array element, dictionary, or string. For-in at Swift is very flexible and secure because it eliminates the risk of out-of-bounds index. If the repetition variable is not required, the developer can replace it with the underscore sign (_).
Key points
- The 'for-in' iteration iterates on each item within the range or data collection
- Use the bottom line '_' as a substitute for the variable if the value is not required
- The 'stride(from:to:by:)' function facilitates repetition with custom step intervals
- Can iterate key-value pairs in the Dictionary simultaneously
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.