Bird
0
0

What is wrong with this Swift struct for form data?

medium📝 Debug Q14 of 15
iOS Swift - User Input and Forms
What is wrong with this Swift struct for form data?
struct ContactForm {
  var phone: String
  var email: String
  init(phone: String) {
    self.phone = phone
  }
}
AProperties must be optional
BMissing initializer for 'email' property
CStructs cannot have initializers
DCannot use 'self' in init
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the struct properties

    ContactForm has two properties: phone and email, both non-optional Strings.
  2. Step 2: Check the initializer

    The init only sets phone, but email is not initialized, causing a compile error.
  3. Final Answer:

    Missing initializer for 'email' property -> Option D
  4. Quick Check:

    All non-optional properties must be initialized [OK]
Quick Trick: Initialize all non-optional properties in init [OK]
Common Mistakes:
  • Forgetting to initialize all properties
  • Thinking 'self' cannot be used in init
  • Believing structs cannot have initializers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes