Swift – Append Integer to Array

Welcome to Swift Tutorial. In this tutorial, we will learn how to append an integer to an array in Swift.

To append an integer to the Integer array in Swift, use the function append() on the array.

Following is a quick example to add an integer to the array using append() function.

array_name.append(number)

where

array_name is the Integer Array

number is the new number appended to this array

After appending, the size of the array increases by 1.

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

After appending an integer, the size of the array has been incremented by 1.

ADVERTISEMENT

Conclusion

In this Swift Tutorial, we have learned to append an integer to Integer Array.