0
0
iOS Swiftmobile~10 mins

Collections (Array, Dictionary, Set) in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AArray()
B{}
C()
D[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces {} which are for dictionaries or closures.
Using parentheses () which are for tuples or function calls.
2fill in blank
medium

Complete 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'
A"Tokyo"
BTokyo
C'Tokyo'
Dtokyo
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string value.
Using single quotes which are not valid for strings in Swift.
3fill in blank
hard

Fix 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'
A["apple", "banana", "cherry"]
BSet(["apple", "banana", "cherry"])
C{"apple", "banana", "cherry"}
D("apple", "banana", "cherry")
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets alone creates an array, not a set.
Using parentheses or curly braces incorrectly.
4fill in blank
hard

Fill 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'
A>
B10
C<
D15
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than < instead of greater than >.
Using the wrong number for comparison.
5fill in blank
hard

Fill 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'
A>
B4
C$0
Dword
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.