Bird
0
0

var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") let jsonBody = ["name": "John"] request.httpBody = try?

medium📝 Predict Output Q4 of 15
iOS Swift - Networking
Given the Swift code below, what will be the value of 'responseString' after the POST request completes successfully? let url = URL(string: "https://example.com/api")! var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") let jsonBody = ["name": "John"] request.httpBody = try? JSONSerialization.data(withJSONObject: jsonBody) // Assume server responds with JSON: {"status":"success"} // After data task, responseString is set to String(data: data, encoding: .utf8)
A{"status":"success"}
B{"name":"John"}
Cnil
DAn error message string
Step-by-Step Solution
Solution:
  1. Step 1: Understand request and response flow

    The request sends JSON with name John; server responds with JSON {"status":"success"}.
  2. Step 2: Convert response data to string

    responseString is created from server response data, so it contains the server's JSON string.
  3. Final Answer:

    {"status":"success"} -> Option A
  4. Quick Check:

    Response string = A [OK]
Quick Trick: Response string shows server reply, not request body [OK]
Common Mistakes:
  • Confusing request body with response
  • Expecting request JSON as response
  • Assuming nil or error without checking

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes