Menu
Coddy logo textTech

The Contains Method

Part of the Fundamentals section of Coddy's Swift journey — lesson 76 of 86.

When working with arrays, you often need to check if a specific value exists before performing an action. Swift provides the contains() method for exactly this purpose—it returns true if the element is found, and false otherwise.

let fruits = ["apple", "banana", "orange"]

if fruits.contains("banana") {
    print("We have bananas!")
}
// We have bananas!

This is much cleaner than writing a loop to search through the array manually. The method works with any type of array, whether it contains strings, numbers, or other values:

let numbers = [10, 20, 30, 40, 50]

print(numbers.contains(30))  // true
print(numbers.contains(99))  // false

A common use case is validating user input against a list of allowed values:

let validOptions = ["yes", "no", "maybe"]
let userInput = "yes"

if validOptions.contains(userInput) {
    print("Valid choice")
} else {
    print("Invalid choice")
}
// Valid choice

The contains() method also works with strings to check if a substring exists within a larger string:

let message = "Hello, Swift!"
print(message.contains("Swift"))  // true
challenge icon

Challenge

Easy

You will receive two lines of input:

  1. A comma-separated list of allowed usernames (e.g., admin,user,guest,moderator)
  2. A username to check

Check if the username exists in the allowed list. If it does, print Access granted. If it doesn't, print Access denied.

Steps:

  1. Read the comma-separated list and split it into an array of strings
  2. Read the username to check
  3. Use the contains() method to determine if the username is in the allowed list
  4. Print the appropriate message based on the result

Hint: Use components(separatedBy:) to split a string into an array. For example:

let text = "a,b,c"
let parts = text.components(separatedBy: ",")

Cheat sheet

The contains() method checks if a specific value exists in an array, returning true if found and false otherwise:

let fruits = ["apple", "banana", "orange"]

if fruits.contains("banana") {
    print("We have bananas!")
}
// We have bananas!

It works with any array type:

let numbers = [10, 20, 30, 40, 50]

print(numbers.contains(30))  // true
print(numbers.contains(99))  // false

Common use case for validating input:

let validOptions = ["yes", "no", "maybe"]
let userInput = "yes"

if validOptions.contains(userInput) {
    print("Valid choice")
} else {
    print("Invalid choice")
}
// Valid choice

The contains() method also works with strings to check for substrings:

let message = "Hello, Swift!"
print(message.contains("Swift"))  // true

To split a string into an array, use components(separatedBy:):

let text = "a,b,c"
let parts = text.components(separatedBy: ",")

Try it yourself

// Read the comma-separated list of allowed usernames
let allowedList = readLine()!

// Read the username to check
let username = readLine()!

// TODO: Write your code below
// 1. Split the allowedList into an array using components(separatedBy:)
// 2. Use contains() to check if username is in the array
// 3. Print "Access granted" or "Access denied" based on the result
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals