Junior — Middle
How are data arrays implemented and operated in Swift?
sobes.tech AI
Answer from AI
In Swift, arrays are represented by the Array type, which is a generic collection that stores an ordered set of elements of the same type.
Features:
- Arrays in Swift are a structure (value type), so they are copied when assigned or passed (copy-on-write optimization).
- You can create mutable (
var) and immutable (let) arrays. - They support indexing, iteration, adding, and removing elements.
Example of creating and using an array:
var numbers: [Int] = [1, 2, 3]
numbers.append(4) // adding an element
print(numbers[0]) // outputs 1
for number in numbers {
print(number)
}
Thus, arrays in Swift are convenient and safe due to type safety and value type features.