The last property returns the last element of the array.
Example
last Syntax
The syntax of the array last property is:
array.last
Here, array is an object of the Array class.
last Return Values
The last property returns the last element of array.
Note: The last property returns an optional value, so we need to unwrap it. There are different techniques to unwrap optionals. To learn more about optionals, visit Swift Optionals.
Example 1: Swift Array last
Output
last Name of Array: Nadal last Even Number: 10
Here, the last element of the names array is "Nadal", so the last property returns "Nadal" string.
Similarly, the last element of the even array is 10, so the property returns 10.
Note: Here we have used ! to force unwrap the optional. To learn more about forced unwrapping, visit Optional Forced Unwrapping.