Swift Arrays
Swift Arrays
Arrays store ordered collections of values of the same type.
Arrays are value types and copy on write.
Create and Access
Create arrays with literals or initializers.
Access elements by index and use properties like count and isEmpty.
Example
var numbers = [10, 20, 30]
print(numbers[0]) // 10
numbers.append(40)
print(numbers.count) // 4
print(numbers.isEmpty) // false
This example creates an array, accesses items, appends, and inspects properties.
Tip: Arrays are value types.
Mutating a copy will not affect the original (copy-on-write semantics).
Insert and Remove
Modify arrays by inserting and removing elements.
Example
var items = ["A", "B", "C"]
items.insert("X", at: 1)
print(items)
items.remove(at: 2)
print(items)