IndexSet is a collection of increasing integers, therefore you can map each index to the corresponding array element:

let array = ["sun", "moon", "star", "meteor"]
let indexSet: IndexSet = [2, 3]

let result = indexSet.map { array[$0] } // Magic happening here!
print(result) // ["star", "meteor"]

This assumes that all indices are valid for the given array. If that is not guaranteed then you can filter the indices (as @dfri correctly remarked):

let result = indexSet.filteredIndexSet { $0 < array.count }.map { array[$0] }
Answer from Martin R on Stack Overflow
People also ask

Can you filter an array in Swift?
Yes, you can filter an array in Swift using the filter function to selectively extract elements that meet specific criteria.
🌐
dhiwise.com
dhiwise.com › post › efficient-data-refinement-with-swift-array-filter-strategies
Mastering Swift Array Filter: A Comprehensive Guide
How do you apply a filter on an array of objects in Swift?
To filter an array of objects in Swift, you can define a filtering condition using a closure that checks specific properties of the objects and then use the filter function to create a new array with the filtered elements.
🌐
dhiwise.com
dhiwise.com › post › efficient-data-refinement-with-swift-array-filter-strategies
Mastering Swift Array Filter: A Comprehensive Guide
How does the filter() function work in Swift?
The filter() function in Swift evaluates each element of an array based on a given condition and returns a new array containing only the elements that satisfy that condition.
🌐
dhiwise.com
dhiwise.com › post › efficient-data-refinement-with-swift-array-filter-strategies
Mastering Swift Array Filter: A Comprehensive Guide
🌐
Programiz
programiz.com › swift-programming › library › array › filter
Swift Array filter() (With Examples)
The filter() method returns all the elements from the array that satisfy the provided condition. The filter() method returns all the elements from the array that satisfy the provided condition. Example var numbers = [2, 3, 6, 9] // return all the elements greater than 5 var result = ...
🌐
Donny Wals
donnywals.com › how-to-filter-an-array-in-swift
How to filter an Array in Swift? – Donny Wals
April 23, 2024 - When you have an Array of elements, and you want to drop all elements that don't match specific criteria from the Array, you're looking for Array's filter(isIncluded:) method.
🌐
DhiWise
dhiwise.com › post › efficient-data-refinement-with-swift-array-filter-strategies
Mastering Swift Array Filter: A Comprehensive Guide
September 10, 2024 - Index-based Filtering: When filtering based on specific index positions or conditions that can be optimized using direct access, prefer index-based filtering over sequential iteration.
🌐
Sarunw
sarunw.com › posts › swift-array-filter
How to Filter an Array in Swift | Sarunw
July 10, 2023 - Swift Array has an instance method, filter(_:), that removes unwanted elements from the resulting array.
Find elsewhere
🌐
Reddit
reddit.com › r/swift › filtering array of objects by strings in another array.
r/swift on Reddit: Filtering array of objects by strings in another array.
October 30, 2015 -

I have an array of Match objects [Match] with a string property of matchID. I also have an array of strings.

I would like to remove the Match object where matchID equals any string in the array.

I currently have:

                var matches = [Match]
                var matchIDsToDelete = [String]

                for match in matches {
                    for id in matchIDsToDelete {
                        if match.matchID == id {
                            // remove that item from array
                        }
                    }
                }

My concern is removing the object from the array as I'm enumerating. I believe there will be consequences. Any suggestions? I would also love a functional solution as I'm currently reading more about functional programming.

Thanks!

🌐
Swift Forums
forums.swift.org › using swift
How to effectively filter array by another array? ( In functional paradigm ) - Using Swift - Swift Forums
December 27, 2018 - Consider following task. Let A be a list of Comparable items. Let B be a list of Comparable items. Entries in these lists can be compared. Output is a list of entries in A that are not in the list B. Two conditions. …
🌐
Mahi Garg
mahigarg.github.io › blogs › filter-operator-swift
Filter Operator: Swift · Mahi Garg
The filter operator is used to create a new collection object by iterating over the existing collection object and filtering the elements based on the predicates to it. The predicate that needs to be checked for, is passed as a higher-order function named isIncluded to the filter function.
🌐
Apple Developer
developer.apple.com › forums › thread › 86114
How to filter one array based on a… | Apple Developer Forums
CustomCell if(searchActive){ cell.toolLabel.text = filtered[indexPath.row] } else { cell.toolLabel.text = data[indexPath.row] } return cell } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let vcName = identities[indexPath.row] let viewController = storyboard?.instantiateViewController(withIdentifier: vcName) self.navigationController?.pushViewController(viewController!, animated: true) } } Any help would be greatly appreciated. ... This seems to be the preferred way. //... //### change the 2 arrays into 1 typealias Item = (data: String, identity: Stri
🌐
Bugfender
bugfender.com › blog › swift-arrays
Swift Arrays: Map, Filter, Reduce & Sort Explained | Bugfender
November 7, 2025 - So in this guide, we’ll go beyond the basics and explore advanced Swift array operations like map, filter, reduce, and sorted, with practical examples and performance notes. By the end, you’ll know how to write cleaner, faster, and more expressive Swift code using arrays.
Top answer
1 of 7
51

contains() checks if a sequence contains a given element, e.g. if a String contains a given Character.

If your intention is to find all books where the name contains the substring "rt", then you can use rangeOfString():

var arr = englishBooks.filter {
    $0.nameOfBook.rangeOfString("rt") != nil
}

or for case-insensitive comparison:

var arr = englishBooks.filter {
    $0.nameOfBook.rangeOfString("rt", options: .CaseInsensitiveSearch) != nil
}

As of Swift 2, you can use

nameOfBook.containsString("rt") // or
nameOfBook.localizedCaseInsensitiveContainsString("rt")

and in Swift 3 this is

nameOfBook.contains("rt") // or
nameOfBook.localizedStandardContains("rt") // or
nameOfBook.range(of: "rt", options: .caseInsensitive) != nil
2 of 7
17

Sorry this is an old thread. Change you code slightly to properly init your variable 'nameOfBook'.

class book{
   var nameOfBook: String!
   init(name: String) {
      nameOfBook = name
   }
}

Then we can create an array of books.

var englishBooks = [book(name: "Big Nose"), book(name: "English Future 
Prime Minister"), book(name: "Phenomenon")]

The array's 'filter' function takes one argument and some logics, 'contains' function can take a simplest form of a string you are searching for.

let list1 = englishBooks.filter { (name) -> Bool in
   name.contains("English")
}

You can then print out list1 like so:

let list2 = arr1.map({ (book) -> String in
   return book.nameOfBook
})
print(list2)

// print ["English Future Prime Minister"]

Above two snippets can be written short hand like so:

let list3 = englishBooks.filter{ ($0.nameOfBook.contains("English")) }
print(list3.map({"\($0.nameOfBook!)"}))
🌐
Cocoa Casts
cocoacasts.com › swift-fundamentals-how-to-find-an-object-in-an-array
How to Find an Object in an Array
The filter(_:) method accepts a closure as its only argument. The closure accepts an object of type Int and its return value is Bool. The resulting array includes every object of the array for which the closure returns true. Like many other programming languages, Swift includes a wide range ...