CODEX

Swift Arrays: Everything You Need to Know

Learn all the basics of working with Swift arrays.

Artturi Jalli
Mar 19 · 4 min read
Photo by Pakata Goh on Unsplash
let numbers = [1, 2, 3, 4, 5]

How to Create an Array

let arr = [1, 2, 3]

How to Count Number of Elements inside an Array

let numbers = [1, 2, 3]
let length = numbers.count
print(length)
3

How to Access an Element in the Array

Array Index

let numbers = [1, 2, 3]let second_number = numbers[1] // returns 2
let third_number = numbers[2] // returns 3

Access a Range of Elements

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]let range = numbers[1...3] // returns [2,3,4]

How to Modify an Array Element

var numbers = [1,2,3]numbers[0] = 10   // Changes the first value to 10
print(numbers)
[10, 2, 3]

How to Add to an Array

1. Append

var nums = [1, 2, 3]nums.append(4)
print(nums)
[1, 2, 3, 4]

2. Insert

var nums = [1,2,3]nums.insert(10, at: 0) // Insert 10 as the first element at pos 0.
print(nums)
[10, 1, 2, 3]

3. Using the += Operator

var nums = [1,2,3]nums += [10, 20, 30]
print(nums)
[1, 2, 3, 10, 20, 30]

How to Remove from an Array

How to Remove Element at Index

var nums = [10, 20, 30]nums.remove(at: 1)  // remove the 2nd element
print(nums)
[10, 30]

How to Remove Elements Based on a Criterion

var nums = [1, 1, 3, 1, 1, 3, 4, 2]nums = nums.filter { $0 != 1 }
print(nums)
[3, 3, 4, 2]

How to Remove the First Element

var nums = [10, 20, 30]nums.removeFirst()
print(nums)
[20, 30]

How to Remove the Last Element

var nums = [10, 20, 30]nums.removeLast()
print(nums)
[10, 20]

How to Remove All Elements in an Array

var nums = [10,20,30]nums.removeAll()
print(nums)
[]

How to Iterate Over an Array

let nums = [1, 2, 3]for number in nums {
print(number)
}
1
2
3

How to Reverse an Array

var numbers = [1, 2, 3]numbers.reverse()
print(numbers)
[3, 2, 1]

How to Sort an Array

var numbers = [3, 1, 2]numbers.sort(by: <)
print(numbers)
[1, 2, 3]
var numbers = [3, 1, 2]numbers.sort(by: >)
print(numbers)
[3, 2, 1]

Conclusion

CodeX

Everything connected with Tech & Code

Artturi Jalli

Written by

Tech Expert, Entrepreneur, Software Engineer from Finland. Writing about programming and science. Check out my blog at https://www.codingem.com/

CodeX

CodeX

Everything connected with Tech & Code

Artturi Jalli

Written by

Tech Expert, Entrepreneur, Software Engineer from Finland. Writing about programming and science. Check out my blog at https://www.codingem.com/

CodeX

CodeX

Everything connected with Tech & Code

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store