0
0
iOS Swiftmobile~10 mins

Image loading from URL 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 a URL from a string.

iOS Swift
let url = URL(string: [1])
Drag options to blanks, or click blank then click option'
Ahttps://example.com/image.png
B"https://example.com/image.png"
CURL(string: "https://example.com/image.png")
DimageURL
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the URL string.
Passing a variable name instead of a string.
2fill in blank
medium

Complete the code to start a data task to download image data.

iOS Swift
URLSession.shared.dataTask(with: url!) { data, response, error in
    if let data = [1] {
        // Use data
    }
}.resume()
Drag options to blanks, or click blank then click option'
Adata
Burl
Cerror
Dresponse
Attempts:
3 left
💡 Hint
Common Mistakes
Checking 'response' or 'error' instead of 'data'.
Forgetting to unwrap the optional data.
3fill in blank
hard

Fix the error in updating the UI image inside the data task.

iOS Swift
DispatchQueue.main.[1] {
    imageView.image = UIImage(data: data)
}
Drag options to blanks, or click blank then click option'
Aasync
Bsync
CasyncAfter
Dbackground
Attempts:
3 left
💡 Hint
Common Mistakes
Using sync which can cause deadlocks.
Updating UI on a background thread.
4fill in blank
hard

Fill both blanks to safely unwrap the URL and start the data task.

iOS Swift
guard let url = [1](string: [2]) else { return }
URLSession.shared.dataTask(with: url).resume()
Drag options to blanks, or click blank then click option'
AURL
BurlString
CURL(string: urlString)
DURLSession
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the whole initializer in one blank.
Using wrong variable names.
5fill in blank
hard

Fill all three blanks to create a UIImage from data and assign it to imageView on the main thread.

iOS Swift
if let data = data {
    DispatchQueue.main.[1] {
        imageView.image = [2](data: [3])
    }
}
Drag options to blanks, or click blank then click option'
Aasync
BUIImage
Cdata
Dsync
Attempts:
3 left
💡 Hint
Common Mistakes
Using sync instead of async.
Forgetting to unwrap data.
Not creating UIImage correctly.