Lesson 7 of 15

Arrays

Arrays

Swift arrays are ordered collections of values of the same type:

var nums = [1, 2, 3, 4, 5]
var names = ["Alice", "Bob", "Charlie"]

Common Operations

nums.append(6)          // add to end
print(nums.count)       // number of elements
print(nums[0])          // access by index (first = 0)
nums[0] = 10            // modify element

Iterating

for num in nums {
    print(num)
}

Your Task

Write a function sum that takes an array of integers and returns their sum.

JS Transpiler loading...
Loading...
Click "Run" to execute your code.