Learn how to iterate over sequences and collections using the standard for-in loop in Swift. Use pattern matching to apply conditional constraints to looping mechanisms.
- [Instructor] Swift provides several ways to loop…or iterate over a sequence or collection.…The most common way is to use a for-in loop.…I'll define an array, and then iterate over it…using a for-in loop.…I is an arbitrary value name,…which I'm using in the print statement.…And i is a constant, so I can't modify it.…If I want to modify it, I need to declare it as a variable.…Just remember that I am modifying…copies of the values in the numbers array,…because it is an array of value types.…
The original numbers array is unmodified.…Iterating over sets works similarly to arrays.…Sets are unordered.…So the order you define the set values in…may not be the order of iteration.…For-in loops also work with dictionaries.…Because dictionaries have key value pairs,…for-in returns a tuple for each key value pair,…which I've decomposed into player and score here.…
Dictionaries are also unordered,…so the iteration order is undetermined.…You may choose to not decompose the tuple.…For example, here is a for-in loop…iterating over the characters of the string SWIFT.…
Learn how to write code, understand Swift's key concepts and best practices, and strengthen your programming problem-solving skills. Instructor Scott Gardner teaches the fundamentals, so you'll be prepared to develop applications for iOS, macOS, and other platforms. Completing this course will enable you to not only write first-class code, but to think like a Swift developer.
- Creating playgrounds
- Defining variables and constants
- Working with characters and strings
- Working with collections and groups
- Using operators and defining custom operators
- Controlling program flow
- Defining functions and closures
- Working with classes, structures, and enumerations
- Adopting protocols
Skill Level Beginner
1. Get Started
2. Variables and Constants
3. Characters and Strings
4. Collections and Tuples
6. Control Flow
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.