Learn how to collect values into ordered lists called arrays, and use various initializers to create new arrays. Understand the tradeoffs of creating an array of different types.
- [Instructor] Swift defines three formal collection types, array, dictionary, and set. Arrays are ordered collections of values in a zero index list, that is, the first item is at index zero. To define an array, declare a variable or constant and assign to it a comma separated list of values enclosed in square brackets. Mutability of an array and the values it holds are determined by whether an array is created as a variable or a constant. threeStooges is mutable because I created it as a variable. I'll get into how you modify an array shortly.
Array should be used to store values of the same type, and the array type will be inferred by the type of its contents when defined if the type is not explicitly stated. threeStooges is inferred to be an array of type String. An array type can also be explicitly stated by writing the array type in square brackets, and initialized to be an empty array of that type by writing empty square brackets. I could also have just declared inningScores without initializing it, and then initialized it later as long as I do so before attempting to use it.
Another syntax is available for initializing an empty array of a specific type. Right the array type followed by empty parenthesis in the assignment. This calls array's default initializer which takes no parameters. The square brackets are actually a syntax shortcut for defining the array type using angle brackets. You may see variations of using shorthand or longhand syntax with arrays, so it's just good to be aware of it. Array variables can also be created with a default size and contents by using array's repeating count initializer.
Counters is a variable array of int values starting out with five zeros. An array's type can also be explicitly defined when using a repeating count initializer. I've made averageScores an array of float values, whereas it would have been inferred to be an array of doubles by default. Arrays can be optionals, meaning that the array value could be nil, or an array containing values. Arrays can hold optionals, meaning that each element can be nil, or contain an optional value of the array type, and arrays can be optionals that hold optionals.
Just remember that arrays cannot hold implicitly unwrapped optionals. Arrays are intended to hold values of the same type. You can force an array to be able to hold values of any type by explicitly defining it as an array of any, but this is generally discouraged because you forfeit type safety and expressive intent. As you can see, there are a lot of different ways to declare and initialize arrays and opinions vary as to the best way. It's good to be familiar with these various forms of syntax, but my advice is to just pick a style that works best for you and be consistent.
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