Swift – Append Integer to Array
Use the append(_:) method to add an integer to the end of a mutable Swift array. The array must be declared with var, and its element type must accept an Int value.
The following syntax appends one integer to an existing Integer Array.
array_name.append(number)
array_nameis a variable of type[Int]orArray<Int>.numberis the integer added as the new last element.- After a successful append operation, the array’s
countincreases by one.
Example 1 – Append an Integer to an Array in Swift
In the following example, we shall define an array with three integers and using append() function, we will append an integer to the array.
main.swift
var numbers:[Int] = [7, 54, 21]
numbers.append(64)
var a = numbers[3]
print( "New size of numbers array is \(numbers.count)" )
print( "Value of integer at index 3 is \(a)" )
Output
New size of numbers array is 4
Value of integer at index 3 is 64
The value 64 becomes the fourth element. Because Swift arrays use zero-based indexing, that element is available at index 3.
Append an Integer and Print the Updated Swift Array
You can print the complete array after calling append(_:) to verify both the element order and the new value.
var scores: [Int] = [18, 25, 31]
scores.append(40)
print(scores)
print(scores.last ?? 0)
Output
[18, 25, 31, 40]
40
The last property returns an optional value because an array can be empty. The nil-coalescing expression scores.last ?? 0 prints 0 only when no last element exists.
Add an Integer at a Specific Array Index in Swift
append(_:) always adds the value at the end of the array. To add an integer at a specific valid index, use insert(_:at:).
var numbers: [Int] = [10, 30, 40]
numbers.insert(20, at: 1)
print(numbers)
Output
[10, 20, 30, 40]
The index passed to insert(_:at:) must be in the range 0...numbers.count. An index outside that range causes a runtime error.
Append Multiple Integers to a Swift Array
When several integers need to be added, use append(contentsOf:) with another sequence of integers.
var numbers: [Int] = [1, 2]
numbers.append(contentsOf: [3, 4, 5])
print(numbers)
Output
[1, 2, 3, 4, 5]
For the API definition and supported overloads, see Apple’s Array append documentation.
Swift Array Append Errors to Check
- The array was declared with
let: constants cannot be modified. Declare the array withvarbefore appending. - The appended value has the wrong type: an array declared as
[Int]accepts integer values, not strings such as"64". - An invalid index is read after appending: the last valid index is
array.count - 1when the array is not empty. append(_:)is assigned to another variable: the method mutates the array and does not return the updated array.
Frequently Asked Questions about Appending Integers in Swift
Can I append an integer to an array declared with let?
No. A Swift array declared with let is immutable. Declare it with var when its elements need to change.
Does append add the integer at a selected index?
No. append(_:) adds the integer only at the end. Use insert(_:at:) to place it at a specific valid index.
How do I append several integers in one operation?
Call append(contentsOf:) and pass an array or another sequence containing values of type Int.
What index contains the newly appended integer?
After appending, the new last element is at array.count - 1. You can also access it safely through the optional array.last property.
Editorial QA Checklist for Swift Integer Array Append Examples
- Confirm that every array modified by
append(_:)is declared withvar. - Confirm that appended values are integers compatible with the array’s
Intelement type. - Verify that examples describe zero-based indexes correctly after the array count changes.
- Keep
append(_:)examples separate frominsert(_:at:)examples so end insertion and indexed insertion are not confused. - Run each code sample and compare its printed output with the documented output block.
Swift Integer Array Append Summary
Use append(_:) to add one integer to the end of a mutable [Int] array. Use append(contentsOf:) for multiple integers and insert(_:at:) when the value must be placed at a particular index. Continue with the main Swift Tutorial for related array operations.
TutorialKart.com