Which Swift code correctly defines a struct to hold a user's name and age?
easy📝 Syntax Q3 of 15
iOS Swift - User Input and Forms
Which Swift code correctly defines a struct to hold a user's name and age?
Aclass User { var name: String; var age: Int }
Bstruct User { var name: String; var age: Int }
Cstruct User { name: String; age: Int }
Dstruct User { var name; var age }
Step-by-Step Solution
Solution:
Step 1: Check struct syntax
Swift structs require 'var' or 'let' before properties with type annotations.
Step 2: Compare options
struct User { var name: String; var age: Int } correctly uses 'var' and types. class User { var name: String; var age: Int } uses class, not struct. Options C and D miss 'var' or types.
Final Answer:
struct User { var name: String; var age: Int } -> Option B
Quick Check:
Correct struct syntax = struct User { var name: String; var age: Int } [OK]
Quick Trick:Struct properties need var/let and type [OK]
Common Mistakes:
Using class instead of struct
Omitting var/let keywords
Missing type annotations
Master "User Input and Forms" in iOS Swift
9 interactive learning modes - each teaches the same concept differently