Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create an empty array of integers.
iOS Swift
var numbers: [Int] = [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces {} which are for dictionaries or closures.
Using parentheses () which are for tuples or function calls.
✗ Incorrect
The correct way to create an empty array is with square brackets [].
2fill in blank
mediumComplete the code to add a new key-value pair to the dictionary.
iOS Swift
var capitals = ["France": "Paris"] capitals["Japan"] = [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string value.
Using single quotes which are not valid for strings in Swift.
✗ Incorrect
Dictionary values must be strings in quotes. "Tokyo" is the correct string literal.
3fill in blank
hardFix the error in the code to create a set of strings.
iOS Swift
let fruits: Set<String> = [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets alone creates an array, not a set.
Using parentheses or curly braces incorrectly.
✗ Incorrect
To create a set with values, use Set([...]) with an array inside.
4fill in blank
hardFill both blanks to create a dictionary comprehension that filters keys with values greater than 10.
iOS Swift
let scores = ["Alice": 12, "Bob": 8, "Eve": 15] let filtered = scores.filter { $0.value [1] [2] }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than < instead of greater than >.
Using the wrong number for comparison.
✗ Incorrect
The filter checks if the value is greater than 10 using > 10.
5fill in blank
hardFill all three blanks to create a dictionary from an array filtering words longer than 4 characters.
iOS Swift
let words = ["apple", "dog", "banana", "cat"] let dict = Dictionary(uniqueKeysWithValues: words.filter { $0.count [1] [2] }.map { [3] in ([3], [3].count) })
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $0 as the key variable name in the map instead of a named variable.
Using less than < instead of greater than > in the filter.
✗ Incorrect
The filter uses > 4 to select words longer than 4 letters. The map uses 'word' as the key.