0
0
iOS Swiftmobile~10 mins

Async sequences 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 declare an async sequence using AsyncStream.

iOS Swift
let numbers = AsyncStream<Int> { continuation in
    continuation.yield(1)
    continuation.yield(2)
    continuation.finish()
}

Task: Use a for await loop to iterate over [1].
Drag options to blanks, or click blank then click option'
Anumbers
BArray
CDispatchQueue
DTask
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to iterate over Array with for await (Array is not async sequence).
Using DispatchQueue or Task instead of the async sequence variable.
2fill in blank
medium

Complete the async function to return an AsyncStream of integers from 1 to 3.

iOS Swift
func makeNumbers() -> AsyncStream<Int> {
    AsyncStream { continuation in
        for i in 1...3 {
            continuation.[1](i)
        }
        continuation.finish()
    }
}
Drag options to blanks, or click blank then click option'
Asend
Byield
Cemit
Dpush
Attempts:
3 left
💡 Hint
Common Mistakes
Using send, emit, or push which are not valid methods for AsyncStream.
3fill in blank
hard

Fix the error in the async sequence iteration by completing the missing keyword.

iOS Swift
func printNumbers() async {
    let stream = AsyncStream<Int> { continuation in
        continuation.yield(10)
        continuation.finish()
    }

    for [1] number in stream {
        print(number)
    }
}
Drag options to blanks, or click blank then click option'
Alet
Basync
Cawait
Dyield
Attempts:
3 left
💡 Hint
Common Mistakes
Using for async or just for which causes errors.
4fill in blank
hard

Fill both blanks to create an AsyncStream that yields strings and finishes properly.

iOS Swift
let greetings = AsyncStream<String> { [1] in
    [2].yield("Hello")
    [2].finish()
}
Drag options to blanks, or click blank then click option'
Acontinuation
Bstream
Ctask
Dsequence
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the parameter and calls causing errors.
5fill in blank
hard

Fill all three blanks to create a filtered async sequence of integers greater than 5.

iOS Swift
let filtered = AsyncStream<Int> { continuation in
    for i in 1...10 {
        if i [1] 5 {
            continuation.[2](i)
        }
    }
    continuation.[3]()
}
Drag options to blanks, or click blank then click option'
A>
Byield
Cfinish
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < or wrong method names causing logic or syntax errors.