Bird
0
0

How can you combine form data from two structs into one dictionary for sending to a server? struct User { var name: String; var age: Int } struct Contact { var email: String; var phone: String? }

hard📝 Application Q9 of 15
iOS Swift - User Input and Forms
How can you combine form data from two structs into one dictionary for sending to a server? struct User { var name: String; var age: Int } struct Contact { var email: String; var phone: String? }
ACreate a dictionary with keys and values from both structs' properties
BUse inheritance to merge structs automatically
CAssign one struct to the other directly
DConvert structs to JSON strings manually
Step-by-Step Solution
Solution:
  1. Step 1: Understand data merging

    Structs are separate types; to combine data, create a dictionary with keys and values from both.
  2. Step 2: Recognize other options' issues

    Inheritance is not supported for structs; direct assignment between structs is invalid; manual JSON conversion is extra work.
  3. Final Answer:

    Create a dictionary with keys and values from both structs' properties -> Option A
  4. Quick Check:

    Combine data = dictionary merge [OK]
Quick Trick: Merge form data into dictionary for server [OK]
Common Mistakes:
  • Trying inheritance with structs
  • Assigning structs directly
  • Skipping dictionary creation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes