Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a simple function that prints a greeting.
iOS Swift
func greet() {
print([1])
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the double quotes around the string.
Trying to call print inside print.
✗ Incorrect
The print function requires a string argument enclosed in double quotes to print text.
2fill in blank
mediumComplete the code to declare a closure that adds two integers.
iOS Swift
let add: (Int, Int) -> Int = [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect closure syntax without 'in'.
Using subtraction instead of addition.
✗ Incorrect
A closure in Swift is defined with curly braces, parameters, 'in', and a return expression.
3fill in blank
hardFix the error in the function that returns a closure capturing a variable.
iOS Swift
func makeIncrementer() -> () -> Int {
var count = 0
return [1] {
count += 1
return count
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'func()' instead of '{' to start the closure.
Adding extra keywords inside the return statement.
✗ Incorrect
The closure should start with an opening brace '{' without extra keywords.
4fill in blank
hardFill both blanks to create a closure that filters even numbers from an array.
iOS Swift
let numbers = [1, 2, 3, 4, 5] let evens = numbers.filter { [1] in [2] % 2 == 0 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names before 'in' and inside the closure.
Using undefined variable names.
✗ Incorrect
The closure parameter name must be consistent; here 'number' is used both times.
5fill in blank
hardFill all three blanks to create a dictionary comprehension filtering values greater than zero.
iOS Swift
let data = ["a": 1, "b": -1, "c": 3] let filtered = data.filter { [1], [2] in [3] > 0 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent parameter names.
Checking the key instead of the value.
✗ Incorrect
The closure parameters are key and value; here 'k' and 'value' are used, and 'value' is checked.