Bird
0
0

What will happen if you try to assign a String to an Int property in a Swift struct for form data? struct FormData { var age: Int } var data = FormData(age: 25) data.age = "thirty"

medium📝 Predict Output Q5 of 15
iOS Swift - User Input and Forms
What will happen if you try to assign a String to an Int property in a Swift struct for form data? struct FormData { var age: Int } var data = FormData(age: 25) data.age = "thirty"
AThe age property becomes optional
BCompile-time error due to type mismatch
CRuntime crash when assigning
DThe string "thirty" is stored in age
Step-by-Step Solution
Solution:
  1. Step 1: Understand Swift type safety

    Swift enforces types strictly, so assigning a String to an Int property is not allowed.
  2. Step 2: Identify error timing

    This type mismatch causes a compile-time error, preventing the app from building.
  3. Final Answer:

    Compile-time error due to type mismatch -> Option B
  4. Quick Check:

    Type mismatch = Compile-time error [OK]
Quick Trick: Swift types must match exactly [OK]
Common Mistakes:
  • Thinking string stores in Int
  • Expecting runtime crash
  • Assuming property becomes optional

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes