
- iOS Tutorial
- iOS - Home
- iOS - Getting Started
- iOS - Environment Setup
- iOS - Objective-C Basics
- iOS - First iPhone Application
- iOS - Actions and Outlets
- iOS - Delegates
- iOS - UI Elements
- iOS - Accelerometer
- iOS - Universal Applications
- iOS - Camera Management
- iOS - Location Handling
- iOS - SQLite Database
- iOS - Sending Email
- iOS - Audio & Video
- iOS - File Handling
- iOS - Accessing Maps
- iOS - In-App Purchase
- iOS - iAd Integration
- iOS - GameKit
- iOS - Storyboards
- iOS - Auto Layouts
- iOS - Twitter & Facebook
- iOS - Memory Management
- iOS - Application Debugging
- iOS Useful Resources
- iOS - Quick Guide
- iOS - Useful Resources
- iOS - Discussion
How to remove Specific Element from a Swift Array?
To remove a specific object from an element in swift, we can use multiple ways of doing it. Let’s see this in the playground with help of an example.
First, let’s create an array of String.
var arrayOfString = ["a","b","c","f"]
We’ll do it with the following methods as shown below:
Method 1 − Using the filter method of the array.
Arrays in swift have a filter method, which filters the array object depending on some conditions and returns an array of new objects.
let modifiedArray = arrayOfString.filter { $0 != "f" } print(modifiedArray)
When we run the above code, we get the following result.
Method 2 − Removing using the indexPath
Now, we’ll use the indexPath of the object to remove it from the array.
if arrayOfString.contains("c") { let index = arrayOfString.firstIndex(of: "c") arrayOfString.remove(at: index!) print(arrayOfString) }
When we run the above code, we get the following result.
Let’s see one more example of both methods, with numbers.
var arry = [1,2,6,44] let modifiedArray = arry.filter { $0 != 6 } print(modifiedArray) if arry.contains(1) { let index = arry.firstIndex(of: 1) arry.remove(at: index!) print(arry) }
We get the following output when we run the above code.
- Related Articles
- How to remove a specific element from array in MongoDB?
- How to remove a specific element from a JSON Array in Java?
- Swift Program to fill an array with a specific element
- Swift Program to Remove an Element from the Set
- Remove a specific element from a LinkedList in Java
- How to delete/remove an element from a C# array?
- Swift Program to Remove Duplicate Elements From an Array
- How can I remove a specific item from an array JavaScript?
- Swift Program to Remove All Occurrences of an Element in an Array
- Swift Program to Remove All the Elements from the Array
- How to remove every Nth element from an array JavaScript?
- How to remove an element from an array in Java
- How to remove an element from Array List in C#?
- How can I remove a specific item from an array in JavaScript
- Remove null element from MongoDB array?

Advertisements