Swift Tutorial
Swift Data Types
Swift Control Flow
Swift Functions
Swift Collections
Swift OOPs
Swift Additional Topics
In Swift, arrays are ordered collections of values. Here's how you can create, store, and manipulate values in arrays:
You can create an empty array of a specific type using the following syntax:
var emptyArray: [Int] = []
Or using the Array initializer:
var anotherEmptyArray = Array<Int>()
You can create an array with default values like this:
var threeInts = Array(repeating: 0, count: 3) // [0, 0, 0]
var fruits = ["apple", "banana", "cherry"]
You can append a single item using the append(_:)
method:
fruits.append("orange") // ["apple", "banana", "cherry", "orange"]
Or append multiple items using the +=
operator:
fruits += ["mango", "grape"] // ["apple", "banana", "cherry", "orange", "mango", "grape"]
Use the insert(_:at:)
method:
fruits.insert("peach", at: 2) // ["apple", "banana", "peach", "cherry", "orange", "mango", "grape"]
You can change the value of an item by referring to its index:
fruits[0] = "red apple" // ["red apple", "banana", "peach", "cherry", "orange", "mango", "grape"]
Or modify a range of items:
fruits[1...3] = ["strawberry", "blueberry", "raspberry"] // ["red apple", "strawberry", "blueberry", "raspberry", "orange", "mango", "grape"]
Remove an element at a specific index:
fruits.remove(at: 1) // ["red apple", "blueberry", "raspberry", "orange", "mango", "grape"]
Remove the last element:
fruits.removeLast() // ["red apple", "blueberry", "raspberry", "orange", "mango"]
Remove all elements:
fruits.removeAll() // []
You can access elements using their index:
let firstFruit = fruits[0] // "red apple"
You can use the count
property to check how many items are in the array:
let numberOfFruits = fruits.count
Arrays in Swift are versatile and provide numerous methods to store, modify, and access values. Keep in mind that arrays are zero-indexed, so the first element is accessed with index 0
, the second with 1
, and so on. Ensure you don't access an index out of bounds, or it will cause a runtime crash.
How to create arrays in Swift:
Description: Arrays in Swift can be created using the Array
type or the shorthand syntax []
.
Code:
var numbers: Array<Int> = [] // or var names: [String] = Array()
Storing and accessing values in Swift arrays:
Description: Values can be stored in arrays, and individual elements can be accessed using subscript notation.
Code:
var fruits = ["Apple", "Banana", "Orange"] let firstFruit = fruits[0]
Adding elements to arrays in Swift:
Description: Elements can be added to the end of an array using the append
method.
Code:
var numbers = [1, 2, 3] numbers.append(4)
Initializing arrays with values in Swift:
Description: Arrays can be initialized with pre-defined values.
Code:
var colors = ["Red", "Green", "Blue"]
Updating values in Swift arrays:
Description: Existing values in an array can be updated by assigning a new value to a specific index.
Code:
var numbers = [1, 2, 3] numbers[1] = 10
Swift array append and insert methods:
Description: The append
method adds elements to the end, and the insert
method inserts elements at a specific index.
Code:
var animals = ["Lion", "Tiger"] animals.append("Leopard") // Appends to the end animals.insert("Cheetah", at: 1) // Inserts at index 1
Removing elements from arrays in Swift:
Description: Elements can be removed using the remove
method or by adjusting the array's size.
Code:
var colors = ["Red", "Green", "Blue"] colors.remove(at: 1) // Removes element at index 1
Working with empty arrays in Swift:
Description: Empty arrays can be created and later populated.
Code:
var emptyArray: [Double] = []
Swift array literals and initialization:
Description: Arrays can be initialized using literals, providing values directly.
Code:
var fruits = ["Apple", "Banana", "Orange"]