How to Use Filter in Swift

Use filter in Swift when you need to keep only the values that match a condition. It is perfect for search results, active users, completed tasks, selected products, and SwiftUI list data.

What you’ll build or solve

You’ll learn how to use filter in Swift with arrays, model collections, dictionaries, and optional chaining patterns. You’ll also know when map or compactMap is a better fit.

When this approach works best

This approach is the right choice when some values should stay and others should be removed.

Common real-world scenarios include:

  • Search results
  • Active users
  • Completed todos
  • Available products
  • Valid records

This is a bad idea when every value should stay but only change shape.

Prerequisites

You only need:

  • Basic Swift arrays
  • Familiarity with closures
  • Understanding of booleans

Step-by-step instructions

Step 1: Filter simple values

The most common use is arrays.

let scores = [95, 88, 76, 60]

let passing = scores.filter { score in
    score >= 80
}

This returns:

[95, 88]

Only matching values remain.

Step 2: Filter model collections

A common real-world use is struct arrays.

struct User {
    let name: String
    let isActive: Bool
}

let users = [
    User(name: "Alex", isActive: true),
    User(name: "Sam", isActive: false)
]

let activeUsers = users.filter { $0.isActive }

This works perfectly for SwiftUI lists.

Step 3: Filter text search results

Search UIs often rely on filter.

let lessons = ["Swift", "SwiftUI", "React"]

let results = lessons.filter {
    $0.lowercased().contains("swift")
}

This creates lightweight local search.

Step 4: Filter dictionaries

Dictionaries can be filtered too.

let settings = ["darkMode": true, "beta": false]

let enabled = settings.filter { _, value in
    value
}

This keeps only matching pairs.

What to look for:

  • filter removes non-matching values
  • Output may be smaller
  • Great for search and lists
  • Perfect for model collections
  • Use map when transforming instead

Examples you can copy

Passing scores

scores.filter { $0 >= 70 }

Active users

users.filter { $0.isActive }

Search text

items.filter { $0.contains(query) }

Common mistakes and how to fix them

Mistake 1: Using filter when transforming values

What the reader might do:

Use filter to format text.

Why it breaks: map is meant for transformation.

Corrected approach:

Use map.

Mistake 2: Forgetting case-insensitive search

What the reader might do:

Search exact text only.

Why it breaks: user search feels broken.

Corrected approach:

Normalize with .lowercased().

Mistake 3: Filtering repeatedly in large views

What the reader might do:

Run expensive filters inside deep SwiftUI body trees.

Why it breaks: performance can suffer.

Corrected approach:

Precompute or memoize filtered results.

Troubleshooting

If results are empty, inspect the condition.

If search feels case-sensitive, normalize both sides.

If performance drops, move filtering out of the hot UI path.

If values should transform, use map.

Quick recap

  • Use filter to keep matching values
  • Great for search and list data
  • Output may be smaller than input
  • Normalize text for search
  • Use map for transformations