| by Arround The Web | No comments

Swift Array – Append

In this Swift guide, we will see the append() method in the Array Collection.

Before going to discuss this method, we will see what an Array is.

Swift Array

Array in Swift is a Collection that stores multiple elements with the same data type.

In Swift, we have to specify the datatype to represent the data type of Array while creating an array. It is also possible to create elements in an Array without specifying any data type.

append()

append() in Array Swift adds the specified element into the array at the end. It is also possible to add the entire array to the existing array at the end. It takes one parameter that represents the Element or Array.

Syntax:

swift_array.append(Element/Array)

 
Parameters:

Element – If you want to add an element to an Existing Swift Array, you need to pass the Element as a parameter.

Array – If you want to add an entire new Array to an Existing Swift Array, you need to pass the Array name as a parameter.

Return:

It returns an Array with added elements.

Note – You have to add the element with respect to the same data type of the existing array.

Let’s explore the examples in detail.

Example 1:

Create a Swift Array that stores two Integer values and adds an element to it using append() method.

//create an Swift Array with 2 integer elements
var swift_array:[Int] = [34,56]

// Actual swift array
print("Actual Swift Array: ", swift_array)

//add 100 to the above array
swift_array.append(100)

// Updated swift array
print("Updated Swift Array: ", swift_array)

 
Output:


100 is appended to the Swift array.

Explanation:

Line -2:


We created an Array named swift_array that holds 2 integer values.

Line -5:


Display the elements in the swift_array.

Line -8:


Now, use the append() method to add value – 100 to the above swift array at the end.

Line-11:


Finally, we displayed the final swift array.

Example 2:

Create a Swift Array that stores two String values and adds an element to it using append() method.

//create an Swift Array with 2 string elements
var swift_array:[String] = ["Hello","Linuxhint"]

// Actual swift array
print("Actual Swift Array: ", swift_array)

//add "Swift" to the above array
swift_array.append("Swift")

// Updated swift array
print("Updated Swift Array: ", swift_array)

 
Output:


So, the string – “swift” is appended to the Swift array.

Explanation:

Line -2:


We created an Array named swift_array that holds 2 string values.

Line -5:


Display the elements in the swift_array.

Line -8:


Now, use the append() method to add string – “swift” to the above swift array at the end.

Line-11:


Finally, we displayed the final swift array.

Example 3:

Create a Swift Array that stores two String values and append another swift array that has 5 strings to it.

//create an Swift Array with 2 string elements
var swift_array:[String] = ["Hello","Linuxhint"]

// Actual swift array
print("Actual Swift Array: ", swift_array)

//add an array that has 5 strings
swift_array.append(contentsOf:["swift1","swift2","swift3","swift4","swift5"])

// Updated swift array
print("Updated Swift Array: ", swift_array)

 
Output:


So the 5 strings in the array are appended at the end of the swift array.

Explanation:

Line -2:


We created an Array named swift_array that holds 2 string values.

Line -5:


Display the elements in the swift_array.

Line -8:


Now, use the append() method to add an array that has 5 strings –

[“swift1″,”swift2″,”swift3″,”swift4″,”swift5”]

Here, we used the contentsOf property to add the strings inside the append() method.

Line-11:


Finally, we displayed the final swift array.

Example 4:

Create an empty swift array and append another swift array that has 5 strings to it.

//create an Swift Array with no elements
var swift_array:[String] = []

// Actual swift array
print("Actual Swift Array: ", swift_array)

//add an array that has 5 strings
swift_array.append(contentsOf:["swift1","swift2","swift3","swift4","swift5"])

// Updated swift array
print("Updated Swift Array: ", swift_array)

 
Output:


The 5 strings in the array are added to the swift array.

Explanation:

Line -2:


We created an empty Array named swift_array with String data type.

Line -5:


Line -8:


Now, use the append() method to add an array that has 5 strings –

[“swift1″,”swift2″,”swift3″,”swift4″,”swift5”]

Here, we used the contentsOf property to add the strings inside the append() method.

Line-11:


Finally, we displayed the final swift array.

Conclusion

So, we saw how to add an element to the swift array using append() method. It adds the specified element into the array at the end. It is also possible to add the entire array to the existing array at the end. It takes one parameter that represents the Element or Array. We discussed 4 different examples to understand this concept better.

Share Button

Source: linuxhint.com

Leave a Reply