Junior — Middle
Kuidas saada Swiftis kogu Int-tüüpi massiivi kõigi arvude summa?
sobes.tech AI
Vastus AI-lt
In Swift, to obtain the sum of all numbers in an array of type Int, you can use the reduce method, which accumulates the array's values into a single final value.
Example:
let numbers = [1, 2, 3, 4, 5]
let sum = numbers.reduce(0, +)
print(sum) // Will output 15
Here, reduce(0, +) means we start from 0 and sequentially add all the elements of the array. This is a simple and efficient way to get the sum of the array elements.