Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a URL session for API calls.
iOS Swift
let session = URLSession[1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .default or .main instead of .shared causes errors.
✗ Incorrect
The URLSession.shared is the shared singleton session used for simple API calls.
2fill in blank
mediumComplete the code to create a URL object from a string.
iOS Swift
guard let url = URL(string: [1]) else { return }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable name without quotes or a URL object instead of a string.
✗ Incorrect
The URL initializer requires a string literal or variable wrapped in quotes.
3fill in blank
hardFix the error in the code to start a data task.
iOS Swift
let task = session.dataTask(with: url) { data, response, error in guard error == [1] else { return } // process data } task.resume()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking error == true or error == false causes logic errors.
✗ Incorrect
When there is no error, the error variable is nil.
4fill in blank
hardFill both blanks to parse JSON data safely.
iOS Swift
if let data = data { let json = try? JSONSerialization.[1](with: data, options: [2]) // use json }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using jsonArray instead of jsonObject or wrong options causes parsing errors.
✗ Incorrect
Use jsonObject to parse JSON and .allowFragments to allow top-level fragments.
5fill in blank
hardFill all three blanks to send a GET request with URLRequest.
iOS Swift
var request = URLRequest(url: [1]) request.httpMethod = [2] request.addValue([3], forHTTPHeaderField: "Content-Type")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using "POST" instead of "GET" or wrong header values.
✗ Incorrect
Use the URL variable for the request URL, set method to "GET", and content type to "application/json".