Complete the code to loop through all elements in the array and print each one.
let fruits = ["Apple", "Banana", "Cherry"] for [1] in fruits { print([1]) }
In a for-in loop, the variable (here fruit) represents each element in the collection.
Complete the code to loop through the dictionary and print each key and value.
let ages = ["Alice": 25, "Bob": 30, "Charlie": 35] for ([1], age) in ages { print("\([1]) is \(age) years old") }
When looping through a dictionary, the first variable is the key (here key), and the second is the value.
Fix the error in the code to correctly loop through the set and print each element.
let colors: Set = ["Red", "Green", "Blue"] for [1] in colors { print([1]) }
The loop variable should represent each element in the set, so 'color' is correct.
Fill both blanks to create a dictionary that maps each word to its length if the length is greater than 3 using reduce.
let words = ["cat", "house", "dog", "elephant"] let lengths = words.reduce(into: [String: Int]()) { result, [1] in if [2] > 3 { result[[1]] = [2] } }
In Swift, to create a dictionary from a collection, use the element as the key and its count as the value.
Fill all three blanks to create a for-in loop that prints each index and element from the array.
let animals = ["dog", "cat", "bird"] for ([1], [2]) in animals.[3]() { print("Index \([1]): \([2])") }
Use 'enumerated()' to get index and element pairs from an array in Swift.