Bird
0
0

How do you correctly declare a Swift struct named Car with a property model of type String?

easy📝 Syntax Q3 of 15
iOS Swift - Swift Language Essentials
How do you correctly declare a Swift struct named Car with a property model of type String?
Aclass Car { var model: String }
Bstruct Car { var model: String }
Cstruct Car() { var model: String }
Dstruct Car { let model }
Step-by-Step Solution
Solution:
  1. Step 1: Identify struct syntax

    In Swift, structs are declared with the keyword struct, followed by the name and curly braces.
  2. Step 2: Define properties

    Properties inside structs must have a type specified, e.g., var model: String.
  3. Step 3: Evaluate options

    struct Car { var model: String } correctly declares a struct with a property. class Car { var model: String } uses class, not a struct. struct Car() { var model: String } has invalid syntax with parentheses after the struct name. struct Car { let model } omits the type for model, which is invalid.
  4. Final Answer:

    struct Car { var model: String } -> Option B
  5. Quick Check:

    Structs use struct Name { } syntax [OK]
Quick Trick: Structs use 'struct Name { var property: Type }' syntax [OK]
Common Mistakes:
  • Using class keyword instead of struct
  • Adding parentheses after struct name
  • Omitting property types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes