Finding sum of elements in Swift array

后端 未结 16 1616
耶瑟儿~
耶瑟儿~ 2020-11-30 18:12

What is the easiest (best) way to find the sum of an array of integers in swift? I have an array called multiples and I would like to know the sum of the multiples.

16条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 18:51

    How about the simple way of

    for (var i = 0; i < n; i++) {
     sum = sum + Int(multiples[i])!
    }
    

    //where n = number of elements in the array

提交回复
热议问题