Closures, higher-order functions, throws and try, Result, ARC, retain cycles and weak or unowned references.
40 cards · basic cards · AI-written, checked twice. Edit anything.
- What is a closure in Swift?
Show answer
A self-contained block of functionality that captures and stores references to variables and constants from its surrounding context.
- What is the simplest closure syntax?
Show answer
{ parameters in statements }
- What does it mean for a closure to capture a value?
Show answer
The closure stores a reference to a variable or constant from its surrounding scope, allowing access even after the scope ends.
- What is an escaping closure?
Show answer
A closure called after the function it was passed to returns, escaping the function's scope.
- What does @escaping indicate?
Show answer
A closure parameter that may be called after the function returns, so it escapes the function's scope.
- What is a trailing closure?
Show answer
A closure passed as the last argument to a function, written outside the parentheses for readability.
- How do closures differ from named functions?
Show answer
Closures are unnamed blocks that capture surrounding context; functions are named and do not capture context by default.
- Can closures have explicit return types?
Show answer
Yes, written as { (parameters) -> ReturnType in statements }.
- What is an autoclosure?
Show answer
A closure automatically created to wrap an expression, marked with @autoclosure, allowing you to omit braces.
- What is a non-escaping closure?
Show answer
A closure guaranteed to be called before the function returns, unable to escape the function's scope (the default).
- What is a higher-order function?
Show answer
A function that takes one or more functions as arguments or returns a function as its result.
- What does map do?
Show answer
Transforms each element in a collection using a closure and returns a new array with the transformed elements.
- What does filter do?
Show answer
Returns a new array containing only the elements that satisfy the condition in the closure.
- What does reduce do?
Show answer
Combines all elements into a single value by applying a closure that takes an accumulated value and the next element.
- What does sorted do?
Show answer
Returns a new array with elements sorted according to the closure that compares two elements.