- 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
Array extension to remove an object by value in Swift
To remove an object by value from an array in Swift, you can create an extension to the Array type. You can then call the function that takes the value to eliminate as a parameter. In this article, we will use the filter function to remove an object passing by value in the Swift language. Let's see some examples of how you can eliminate an object by value from an array
Example 1: Remove an Object Using the Filter Function
You can use the built-in filter method to create a new array without the matching value.
Algorithm
Step 1 − Create an extension to an array
Step 2 − Use the where clause to define the element type as Equatable
Step 3 − Define a function called remove(:) by taking the element as an argument
Step 4 − Use the filter function to filter out the element excluding the argument
Example
import Foundation extension Array where Element: Equatable { mutating func remove(_ element: Element) { self = filter { $0 != element } } } var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print("Original array: \(numbers)") numbers.remove(3) print("Modified array: \(numbers)")
Output
Original array: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Modified array: [0, 1, 2, 4, 5, 6, 7, 8, 9]
Keep in mind that the remove function can change the initial array because it is designated as modifying. In this instance, it substitutes a new array for the old one that omits the requested value.
Example 2: Remove a Custom Struct Object Using the Filter Function
In this example, we will remove a custom object (e.g. Struct) from an array using the below steps.
Algorithm
Step 1 − Create an extension to an array
Step 2 − Use the where clause to define the element type as Equatable
Step 3 − Define a function called remove(:) by taking the element as an argument
Step 4 − Use the filter function to filter out the element excluding the argument
import Foundation extension Array where Element: Equatable { mutating func remove(_ element: Element) { self = filter { $0 != element } } } struct Person: Equatable { let name: String let age: Int } var people = [ Person(name: "Alice", age: 30), Person(name: "Bob", age: 25), Person(name: "Charlie", age: 35), Person(name: "Dave", age: 20) ] print("Original array count: \(people.count)") // Remove a person with a specific name and age let personToRemove = Person(name: "Bob", age: 25) print("Removing person: \(personToRemove)") people.remove(personToRemove) print("Modified array count: \(people.count)")
Here is the Output
Original array count: 4 Removing person: Person(name: "Bob", age: 25) Modified array count: 3
After creating the extension along with a method named "remove" and conforming to the Equatable protocol, we are removing an object called "personToRemove" with the name "Bob" and age "25". The person conforms to the Equatable protocol, so the extension knows how to compare objects and remove them when a match is found between the objects.
Conclusion
In conclusion, adding an extension to the Swift Array type can be a potent approach to expand the language's potential. We may remove objects from an array depending on any criteria by developing an extension that provides a remove method to arrays containing Equatable members. This allows us a great deal of flexibility when defining the criteria for removal and what we can remove.
The addon creates a new array sans the matched value using the built-in filter technique. We must designate the delete method as mutating in order to permit it to affect the initial array because we are generating a new array rather than altering the one that already exists.
To Continue Learning Please Login
Login with Google