
This is a course introduction.
This lecture introduces recursion as a fundamental programming technique in Swift, where a function calls itself to solve a problem by breaking it into smaller subproblems. It covers the essential structure of a recursive function, explaining the roles of the base case (to stop recursion) and the recursive case (to continue it). Through the example of calculating a factorial, the lecture demonstrates how recursion operates in practice. Finally, it explores real-world applications in iOS development, such as working with data structures and building nested views in SwiftUI. This foundational knowledge equips developers to write cleaner, more efficient solutions using recursion.
This lecture explains the concept of factorials, a mathematical operation that multiplies all positive integers from 1 up to a given number. Denoted by an exclamation mark (e.g., 5!), the factorial of a number n is defined as n × (n - 1) × ... × 2 × 1, with 0! specifically defined as 1. Through concrete examples, such as 3! = 6 and 5! = 120, the lecture illustrates how factorials work. It also highlights practical uses of factorials in fields like combinatorics, probability, statistics, and computer science, particularly in recursive algorithm design.
In this lecture, you'll learn what a factorial is, how to compute it, and where it is commonly used in both mathematics and programming—especially in algorithmic and recursive logic.
This lecture explores how recursion extends beyond theory into practical, real-world use in Swift and iOS development. It covers key scenarios where recursion offers elegant, efficient solutions—such as traversing tree-like data structures, parsing nested JSON, and managing view hierarchies in SwiftUI. Students will learn how recursive techniques enhance code clarity and simplify complex workflows, from rendering nested comments to implementing search algorithms and game logic. By the end of the lecture, learners will understand how to recognize and apply recursion effectively in their own apps and projects.
This lecture explains what a fibonacci sequence is.
In this lecture, you’ll learn how to write a recursive function that determines whether a given integer appears in the Fibonacci sequence. We'll explore how recursion can be applied not just to generate sequences, but to check membership in them using clean, expressive logic.
This lecture demonstrates coding the solution.
In this lecture, you'll learn how to use recursion to process an HTML-like string. You'll write a recursive parser that walks through nested tags and extracts meaningful content. This mirrors how real HTML parsers work and demonstrates the power of recursion in processing hierarchical text data.
Natural numbers are the numbers you naturally start counting with.
This function calculates the sum of the first n natural numbers recursively:
The base case is when n is 1 or 0. At that point, it simply returns n.
For all values greater than 1, it returns n + sum(n - 1).
This means it keeps calling itself with smaller and smaller values until it reaches 1 or 0, and then adds the results back together.
This video breaks down exactly how recursion works using a single, focused Swift example: the countDigits function.
We’ll walk through each step of how the function processes an integer, how the value of the input changes across recursive calls, and why each call adds only one to the result. If you've ever been confused by recursion or wondered what’s really happening inside a recursive function, this example will make it click.
By the end, you'll understand:
How recursive calls stack and return
Why countDigits uses 1 + countDigits(num / 10)
How each call contributes exactly one to the final total
This is a beginner-friendly breakdown aimed at Swift learners who want a clear, visual, and no-nonsense understanding of recursion — with zero distractions.
In this coding challenge, you’ll write a recursive Swift function that counts how many times a specific digit appears in a given integer.
The function breaks the number down one digit at a time, using modulus and division, and checks each digit against the target. With each recursive call, it strips off the last digit and continues the count, only adding to the total when a match is found.
This problem reinforces:
Integer manipulation (% and /)
Base and recursive cases
Conditional recursion
How recursion replaces iteration when evaluating each part of a number
It's a great next step after problems like countDigits, and an excellent exercise for strengthening your understanding of how recursion works in Swift when processing integers digit by digit.
In this video, we walk through the recursive solution to a classic Swift coding problem: counting how many times a specific digit appears in an integer.
You’ll learn:
How to use integer division and modulus to isolate digits
How recursion can replace loops in digit-based problems
How and when the recursive call adds to the total
How the base case stops the recursion safely
We’ll trace each step clearly so you can see how the number is broken down and evaluated one digit at a time. Whether you're a beginner or reviewing recursion fundamentals, this video will help solidify your understanding of how recursive calls work in Swift with numeric logic.
In this video, we explore how to reverse a string in Swift using recursion — without using any built-in methods like reversed().
You’ll learn:
How to break a string down character by character
How the recursive call processes the string from front to back
How characters are reassembled in reverse order as the call stack unwinds
We’ll walk through a clear, real-world example of recursion, showing exactly how reverseString("hello") becomes "olleh" step-by-step. If you’re learning Swift and want to build intuition around recursion, this video gives you a simple, powerful use case to build on.
No prior recursion knowledge required — this lesson is designed for Swift learners who want to understand how recursion works from the inside out.
In this challenge, you'll write a recursive Swift function that calculates the sum of all integers in an array, without using loops or built-in methods like reduce.
The function works by peeling off the first element of the array on each call, then adding it to the result of a recursive call on the rest of the array. The recursion ends when the array is empty.
This problem helps you:
Practice base and recursive case logic
Work with arrays in a recursive context
Strengthen your understanding of how data is consumed over recursive calls
In this video, we walk through how to solve a classic Swift coding problem: summing all elements in an array using recursion — without using loops or built-in methods like reduce.
You’ll learn:
How to break an array down recursively
What the base case and recursive case look like in array-based recursion
How to use dropFirst() to process elements step by step
How recursion builds the total as the call stack unwinds
This is a great example for Swift learners who are building comfort with recursion and want to see how it works with real data structures like arrays.
In this video, we walk through how to solve a classic string-based coding challenge in Swift: checking if a string is a palindrome using recursion.
You’ll learn:
How to define the base case for string recursion
How to compare the first and last characters of a string
How to reduce a string step-by-step using dropFirst() and dropLast()
How recursion naturally replaces looping in this type of character comparison
We’ll explain each part of the solution clearly so you can understand the logic behind the recursive structure — and apply the same thinking to similar string-processing problems.
By the end, you'll understand how and why recursion is a clean way to handle symmetrical logic like palindromes. Perfect for Swift learners looking to sharpen their recursion skills with real-world examples.
Write a recursive Swift function that counts how many total elements are in a nested array of integers.
The array may contain:
Integers
Other arrays of integers (nested to any depth)
Write a recursive Swift function that counts how many total elements are in a nested array of integers.
The array may contain:
Integers
Other arrays of integers (nested to any depth)
Problem Statement:
Write a recursive function in Swift that checks whether a given target value exists in an array of integers.
You may not use loops or the built-in contains method.
Problem Statement:
Write a recursive function in Swift that checks whether a given target value exists in an array of integers.
You may not use loops or the built-in contains method.
Write a recursive Swift function that finds the largest integer in a nested array.
The input array can contain:
Integers
Other arrays of integers (nested to any depth)
Write a recursive Swift function that finds the largest integer in a nested array.
The input array can contain:
Integers
Other arrays of integers (nested to any depth)
Write a recursive Swift function that finds the largest integer in a nested array.
The input array can contain:
Integers
Other arrays of integers (nested to any depth)
How It Works
Base Case:
if text.isEmpty { return "" }
If the string is empty, return an empty string.
Recursive Case:
Check the first character.
If it matches the target, skip it and recurse on the rest.
If it doesn’t match, keep it and append the recursive result of the rest.
Example: "banana" with target "a"
"b" is not "a" → keep → "b" + recurse("anana")
"a" is "a" → skip → recurse("nana")
"n" is not "a" → keep → "n" + recurse("ana")
"a" is "a" → skip → recurse("na")
"n" is not "a" → keep → "n" + recurse("a")
"a" is "a" → skip → recurse("")
"" → base case → ""
Final result: "bnn"
Problem Statement:
Write a recursive Swift function that checks if an array of integers is sorted in non-decreasing order.
Do not use loops or built-in sort functions.
Recursive Function: isSorted
func isSorted(_ array: [Int]) -> Bool {
if array.count <= 1 {
return true
} else if array[0] > array[1] {
return false
} else {
return isSorted(Array(array.dropFirst()))
}
}
Example Usage:
print(isSorted([1, 2, 3, 4, 5])) // true
print(isSorted([1, 2, 2, 3])) // true
print(isSorted([5, 4, 3, 2, 1])) // false
print(isSorted([42])) // true
print(isSorted([])) // true
How It Works:
Base Case:
if array.count <= 1 { return true }
An array with 0 or 1 element is always sorted.
Recursive Case:
Compare the first two elements.
If array[0] > array[1], the array is not sorted → return false.
Otherwise, drop the first element and recursively check the rest:
isSorted(Array(array.dropFirst()))
Write a recursive Swift function that counts how many times a specific substring appears in a given string. Overlapping occurrences should not be counted.
Recursive Function: countOccurrences
func countOccurrences(of pattern: String, in text: String) -> Int {
if text.count < pattern.count || pattern.isEmpty {
return 0
}
if text.hasPrefix(pattern) {
let nextIndex = text.index(text.startIndex, offsetBy: pattern.count)
let remaining = String(text[nextIndex...])
return 1 + countOccurrences(of: pattern, in: remaining)
} else {
let nextIndex = text.index(after: text.startIndex)
let remaining = String(text[nextIndex...])
return countOccurrences(of: pattern, in: remaining)
}
}
Example Usage
print(countOccurrences(of: "ana", in: "banana")) // 1
print(countOccurrences(of: "aa", in: "aaaaaa")) // 3
print(countOccurrences(of: "abc", in: "abcabcabc")) // 3
print(countOccurrences(of: "x", in: "")) // 0
How It Works
Base Case:
If the text is shorter than the pattern, or the pattern is empty, return 0.
Recursive Case:
If the text starts with the pattern (hasPrefix), we count 1 and skip ahead by the pattern's length.
Otherwise, we skip ahead by one character and continue checking.
This ensures we count non-overlapping matches only.
Here’s the recursive Swift solution for repeating a string n times:
Recursive Function: repeatString
func repeatString(_ str: String, times n: Int) -> String {
if n <= 0 {
return ""
} else {
return str + repeatString(str, times: n - 1)
}
}
Example Usage
print(repeatString("hi", times: 3)) // "hihihi"
print(repeatString("a", times: 5)) // "aaaaa"
print(repeatString("abc", times: 0)) // ""
print(repeatString("!", times: 1)) // "!"
How It Works
Base Case:
If n <= 0, return an empty string.
Recursive Case:
Add one instance of the string, and recursively call with n - 1.
Each level of recursion adds one more copy of the string until n reaches zero.
Write a recursive Swift function that returns a new string with all letters capitalized. You may not use the built-in uppercased() method.
Code Block:
let firstChar = text.first!
let upperChar: Character
if let ascii = firstChar.asciiValue, ascii >= 97 && ascii <= 122 {
upperChar = Character(UnicodeScalar(ascii - 32))
} else {
upperChar = firstChar
}
What Each Line Does
1. let firstChar = text.first!
This grabs the first character of the text string.
text.first returns an optional Character?, so we force unwrap it using ! because we’ve already checked that text is not empty.
2. let upperChar: Character
Declares a variable to store the uppercase version of firstChar.
3. if let ascii = firstChar.asciiValue, ascii >= 97 && ascii <= 122
firstChar.asciiValue converts the character to its ASCII numeric value (as a UInt8), if available.
The condition checks if the ASCII value is between 97 and 122, which corresponds to lowercase letters 'a' to 'z'.
4. upperChar = Character(UnicodeScalar(ascii - 32))
If it is a lowercase letter, we convert it to uppercase by subtracting 32 from its ASCII value.
This works because, in ASCII:
'a' = 97 → 'A' = 65
'b' = 98 → 'B' = 66
...
UnicodeScalar(...) converts the number back into a character.
5. else { upperChar = firstChar }
If the character is not a lowercase letter (maybe it's already uppercase, a digit, or punctuation), we just keep it as-is.
Purpose of This Block
It performs a manual uppercase transformation for a single character without using built-in methods like .uppercased() — which fits the constraint of the recursive challenge.
Are you self-taught or bootcamp-trained — and feel like recursion is still a mystery?
This course was built for you.
If you've tried to learn recursion from YouTube or textbooks but walked away confused, you're not alone. Most resources either rush through the topic or assume you already have a computer science degree. This course doesn't do that.
Instead, Recursion Fundamentals in Swift takes a slow, clear, and example-driven approach to help you understand exactly what recursion is, how it works under the hood, and how to confidently apply it in real-world Swift development.
Who This Course Is For:
Aspiring iOS developers who learned Swift through bootcamps or self-study
Junior developers preparing for technical interviews
Mobile devs who want to strengthen their problem-solving skills
Anyone who's struggled to "get" recursion in a practical way
What You’ll Learn:
What recursion actually is — and why it's not as scary as it sounds
How to trace recursive calls step-by-step and understand the call stack
How to write recursive functions using clean base and recursive cases
Common recursion patterns: string building, array processing, tree-like structures
How to avoid common mistakes like infinite recursion or stack overflows
Practical use cases in Swift: working with strings, arrays, file systems, and more
How to spot when recursion is a better fit than iteration
How to use recursion to answer coding interview questions with confidence
Challenges You'll Solve:
Count digits in an integer
Reverse strings recursively
Search arrays for values recursively
Remove characters from strings
Traverse nested arrays
And more — with step-by-step explanations
Why This Course is Different:
You're not just copying code — you're building understanding. By the end of this course, recursion will stop being a buzzword and start being a tool you can actually use. You'll feel confident explaining your code and tackling recursive problems in interviews — without memorizing solutions.
Whether you're aiming to become an iOS developer or just want to fill a crucial gap in your Swift knowledge, this course gives you the solid foundation you need — without requiring a computer science degree.