Arrays help you keep many items together in one place. Type inference means Swift can guess what kind of items are inside the array without you saying it out loud.
Array creation and type inference in Swift
var arrayName = [item1, item2, item3] // or var arrayName: [Type] = [item1, item2, item3] // or var emptyArray = [Type]()
You can let Swift guess the type by just writing the items inside square brackets.
If you want an empty array, you must tell Swift what type it will hold.
var fruits = ["Apple", "Banana", "Cherry"]
var numbers: [Int] = [1, 2, 3, 4]
var emptyDoubles = [Double]()
// var mixedArray = ["Hello", 5] // This will cause an errorThis program shows how to create arrays with and without type inference. It also shows adding items to arrays and printing them.
import Foundation // Create an array with type inference var colors = ["Red", "Green", "Blue"] print("Colors array before adding:", colors) // Add a new color colors.append("Yellow") print("Colors array after adding:", colors) // Create an empty array of integers var scores = [Int]() print("Scores array initially empty:", scores) // Add scores scores.append(10) scores.append(20) print("Scores array after adding:", scores)
Type inference helps write less code and keeps it clean.
Arrays must hold items of the same type; mixing types causes errors.
Creating an empty array requires specifying the type so Swift knows what to expect.
Arrays store lists of items together.
Swift can guess the type of items in an array if you provide items when creating it.
Empty arrays need a type declared so Swift knows what kind of items will go inside.
Practice
[1, 2, 3] without specifying a type?Solution
Step 1: Understand type inference with array literals
When you write an array with values like [1, 2, 3], Swift looks at the values and guesses the type. Here, all values are integers.Step 2: Determine the inferred type
Since all values are integers, Swift infers the array type as [Int]. No explicit type declaration is needed.Final Answer:
Swift infers the array type as [Int] automatically. -> Option AQuick Check:
Array with integers = [Int] [OK]
- Thinking Swift defaults to [Any]
- Assuming a syntax error without explicit type
- Believing empty array is created instead
Solution
Step 1: Recognize empty array syntax with type annotation
Empty arrays need a type so Swift knows what kind of items will be stored. let names: [String] = [] declares names as [String] and assigns an empty array.Step 2: Check other options for correctness
let names = [] lacks type info, so it's invalid. let names = String[] uses invalid syntax. let names = Array() creates an empty array but type is not specified, so it defaults to [Any].Final Answer:
let names: [String] = [] -> Option CQuick Check:
Empty array needs type annotation [OK]
- Omitting type for empty arrays
- Using Array() without type annotation
- Assuming [] alone is enough
let numbers = [1, 2.5, 3] in Swift?Solution
Step 1: Analyze the array elements' types
The array contains 1 (Int), 2.5 (Double), and 3 (Int). Swift will promote Int values to Double to keep the array type consistent.Step 2: Determine the inferred array type
Because of the Double value 2.5, Swift infers the array type as [Double], converting Ints to Double.Final Answer:
[Double] -> Option BQuick Check:
Mixed Int and Double = [Double] [OK]
- Assuming array is [Int]
- Expecting a syntax error for mixed types
- Thinking array becomes [Float]
let items = []
items.append("apple")Solution
Step 1: Check array declaration and type inference
The array is declared as let items = [] with no type or initial values. Swift cannot guess the type from an empty array literal.Step 2: Understand why append fails
Because items is declared with let, it is immutable and cannot be appended to. To append, it must be declared with var.Final Answer:
Cannot append to a constant array declared with let. -> Option AQuick Check:
let arrays are immutable [OK]
- Thinking append fails because array type is unknown
- Assuming default array type is [Int]
- Believing code runs without error
Solution
Step 1: Understand Swift's type system for mixed arrays
Swift does not support union types like Int | String. To hold different types, use the type Any.Step 2: Check each option for correctness
let mixed: [Any] = [1, "two", 3] declares mixed as [Any] and initializes with Int and String values, which is valid. let mixed = [1, "two", 3] tries to infer type but fails due to mixed types. Options A and C use invalid syntax for union types.Final Answer:
let mixed: [Any] = [1, "two", 3] -> Option DQuick Check:
Mixed types array uses [Any] [OK]
- Trying to infer mixed types without [Any]
- Using invalid union type syntax
- Assuming Swift allows Int | String type
Ready to prove it?
Pass the test and this pattern moves unknown
