- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to return the first 5 objects of an array in Swift?
In Swift to get the first N objects of an array, we can use the prefix function or Range operator. This prefix function is used to retrieve the prefix elements by passing the count limit. Also, you can use the range operator to get n number of elements from an array. Let's see some examples.
In Swift, the prefix function returns an array containing the first n elements.
Using the Prefix Function
You can use the prefix function to get the first n elements of an array.
Step 1 − Create an input array
Step 2 − Call the prefix() function to the input array with the length you want
Step 3 − Convert the returned slice of the array from the prefix function using Array() constructor
Example
Here is a Basic Example of Getting the First Five Elements
let numberArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] let firstFive = Array(numberArray.prefix(5)) print("Original array: \(numberArray)") print("Output array with the first five elements: \(firstFive)")
Output
Original array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Output array with the first five elements: [1, 2, 3, 4, 5]
Using the Prefix Function when the Array Length is Less than the Expected Length
As long as the input array contains fewer elements than the target limit, the prefix function returns all the elements in the array. Here is an example −
Example
let numberArray = [1, 2, 3] let firstFive = Array(numberArray.prefix(5)) print("Original array: \(numberArray)") print("Output array with the first five elements: \(firstFive)")
Output
Original array: [1, 2, 3] Output array with the first five elements: [1, 2, 3]
Using the Prefix Function When You Pass the Higher Limit than the Array Size
When you pass the higher limit in the prefix function than the array size, the prefix function returns all elements.
Example
Here is an example
let numberArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] let firstFive = Array(numberArray.prefix(12)) print("Original array: \(numberArray)") print("Output array: \(firstFive)")
Output
Original array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Output array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
To get elements from an array, the prefix function provides safety if the parameters of the array are exceeded. This is a safe approach to getting elements.
Using Range Operator
You can use the range operator to get the first n elements of an array.
Step 1 − Create an input array
Step 2 − Use the range with lower and upper limits using subscript
Step 3 − Assign the new output array to a new variable
Example
let numberArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] let firstFive = numberArray[0..<5] print("Original array: \(numberArray)") print("Output array: \(firstFive)")
Output
Original array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Output array: [1, 2, 3, 4, 5]
Note that the code will crash if the range operator is exceeded.
Conclusion
Swift provides you with the prefix function to get the n elements from an array. In the event that the limit is exceeded, the prefix function is safe. As a result, your code won't crash if it exceeds the array size limit.
Swift also supports N-element arrays using the range operator. Nevertheless, if the limit is exceeded beyond the array size, your program will crash.
To Continue Learning Please Login
Login with Google