Arrays & Sets
Learn how to use Swift arrays and sets to keep your data organized.
StartKey Concepts
Review core concepts you need to learn to master this subject
Array
Initialize with Array Literal
Index
.count
Property
.append()
Method and +=
Operator
.insert()
and .remove()
Methods
Iterating Over an Array
Swift Sets
Array
Array
var scores = [Int]()
// The array is empty: []
An array stores an ordered collection of values of the same data type.
Use the initializer syntax, [Type]()
, to create an empty array of a certain type.
Arrays
Lesson 1 of 2
- 1We live in an increasingly connected world where information flows between us and the apps that we interact with every day. Historically that information was stored in filing cabinets but, today, mβ¦
- 2In Swift, an array stores values of the same type in an ordered list. To create an empty array of a certain type, we can use the initializer syntax: var name = Type Suppose we want to creaβ¦
- 3We learned one way to create an array, but thereβs another. We can also declare and initialize an array with a list of values, separated by commas, and surrounded by a pair of square brackets. Thβ¦
- 5Properties in Swift, allow us to access a value that is specific to a particular structure. We can use the .count property oβ¦
- 6When working with arrays, we might need to add additional elements to it after initialization. We can add a new item to the end of an array by calling the arrayβs [.append()](https://developer.appβ¦
- 7Previously, we learned how to add an item to the end of the array, but how do we add an item in the middle or even the start? And how do we remove an item? Suppose we have an array: var moon = [ββ¦
- 8What happens if we want to iterate through items of an array? We can use a for-in loop! The syntax looks like the following: for item in array { // Loop body } For example, suppose we have a β¦
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory