Apple Compatibility

Swift Array – First

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

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 can also be possible to create elements in an Array without specifying any data type.

first property

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

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

Syntax:

swift_array.first

 
It doesn’t take any parameters.

swift_array is the array input.

Return:

If the Array is empty, the first 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 first property to get the first 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 first element
var res=swift_array.first

print("First Element:",res!)

 
Output:


As the first element is 23, 23 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 first property to get the first element in the swift_array. The first element is stored in the res variable.

Line-10:


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

Example 2:

Create a Swift Array that stores three String values and use the first property to get the first element in that Swift array.

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

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

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

print("First Element:",res!)

Output:


As the first element is swift1, it is returned.

Explanation:

Line -2:


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

Line -5:


Display the elements in the swift_array.

Line -8:


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

Line-10:


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

Conclusion

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

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain