0
0
iOS Swiftmobile~10 mins

Why API integration connects to servers in iOS Swift - Test Your Understanding

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

Complete 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'
A.main
B.background
C.default
D.shared
Attempts:
3 left
💡 Hint
Common Mistakes
Using .default or .main instead of .shared causes errors.
2fill in blank
medium

Complete 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'
A"https://api.example.com/data"
Bhttps://api.example.com/data
CurlString
DURL(string: "https://api.example.com/data")
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable name without quotes or a URL object instead of a string.
3fill in blank
hard

Fix 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'
A0
Btrue
Cnil
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Checking error == true or error == false causes logic errors.
4fill in blank
hard

Fill 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'
AjsonObject
BjsonArray
C.allowFragments
DmutableContainers
Attempts:
3 left
💡 Hint
Common Mistakes
Using jsonArray instead of jsonObject or wrong options causes parsing errors.
5fill in blank
hard

Fill 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'
Aurl
B"GET"
C"application/json"
D"POST"
Attempts:
3 left
💡 Hint
Common Mistakes
Using "POST" instead of "GET" or wrong header values.