| by Arround The Web | No comments

Swift Array – Last

In this Swift guide, we will see what the last property in the Array Collection is.

Before going to discuss this property, 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.

last property

last in Swift Array is a property which is used to get the last elements present in the given Array.

If the Array is empty, then the last property returns nil Fatal error. So, make sure that Swift Array contains at least one element to implement this property.

Syntax:

swift_array.last

 
It doesn’t take any parameters.

swift_array is the array input.

Return:

If the Array is empty, the last property returns nil Fatal error.


Let’s explore the examples in detail.

Example 1:

Create a Swift Array that stores five Integer values and use the last property to get the last element in that Swift array.

//create an Swift Array with 5 integer elements
var swift_array:[Int] = [23,45,67,4,45]

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

// get the last element
var res=swift_array.last

print("Last Element:",res!)

 
Output:


As the last element is 45, 45 is returned.

Explanation:

Line -2:


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

Line -5:


Display the elements in the swift_array.

Line -8:


Now, use the last property to get the last element in the swift_array. The last element is stored in the res variable.

Line-10:


Finally, we will use the res variable to display the last element. In Swift, we can display the result using ‘!’.

Example 2:

Create a Swift Array that stores five String values. Use the last property to get the last element in that Swift array.

//create an Swift Array with 5 String elements
var swift_array:[String] = ["swift1","swift2","swift3","Linuxhint","sravan"]

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

// get the last element
var res=swift_array.last

print("Last Element:",res!)

 
Output:


As the last element is sravan, it is returned.

Explanation:

Line -2:


We created an Array named swift_array that holds 5 string elements.

Line -5:


Display the elements in the swift_array.

Line -8:


Now, use the last property to get the last element in the swift_array. The last element is stored in the res variable.

Line-10:


Finally, we will display the res variable to display the last element. In Swift we can display the result using ‘!’.

Conclusion

In this Swift tutorial, we saw how to get the last element in Swift Array using the last property. last in Swift Array is a property which is used to get the last elements present in the given Array. If the Array is empty, then the last property returns nil Fatal error. So, make sure that Swift Array contains at least one element to implement this property.

Share Button

Source: linuxhint.com

Leave a Reply