Bird
0
0

You need to send a POST request with nested JSON data in Swift. Which approach correctly creates the JSON body for this dictionary? let data = ["user": ["name": "Alice", "age": 30]]

hard📝 Application Q9 of 15
iOS Swift - Networking
You need to send a POST request with nested JSON data in Swift. Which approach correctly creates the JSON body for this dictionary? let data = ["user": ["name": "Alice", "age": 30]]
AFlatten nested dictionary before encoding
BManually convert nested dictionary to string and then to Data
CUse JSONSerialization.data(withJSONObject: data) to convert nested dictionary to Data
DEncode only the outer dictionary keys as JSON
Step-by-Step Solution
Solution:
  1. Step 1: Understand JSONSerialization with nested dictionaries

    It supports nested dictionaries and converts them correctly to JSON Data.
  2. Step 2: Use data(withJSONObject:) directly

    Passing the nested dictionary directly is correct and efficient.
  3. Final Answer:

    Use JSONSerialization.data(withJSONObject: data) to convert nested dictionary to Data -> Option C
  4. Quick Check:

    Nested JSON encoding = C [OK]
Quick Trick: JSONSerialization handles nested dictionaries automatically [OK]
Common Mistakes:
  • Manually converting nested dictionaries
  • Flattening unnecessarily
  • Encoding only outer keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes