Bird
0
0

How do you correctly declare a @Published property called score of type Int inside a Swift class conforming to ObservableObject?

easy📝 Syntax Q3 of 15
iOS Swift - State Management in SwiftUI
How do you correctly declare a @Published property called score of type Int inside a Swift class conforming to ObservableObject?
Aclass Game: ObservableObject { published var score: Int = 0 }
Bclass Game: ObservableObject { var @Published score: Int = 0 }
Cclass Game: ObservableObject { @Published score: Int = 0 }
Dclass Game: ObservableObject { @Published var score: Int = 0 }
Step-by-Step Solution
Solution:
  1. Step 1: Syntax of @Published

    The @Published attribute precedes the property declaration.
  2. Step 2: Correct property declaration

    Use @Published var score: Int = 0 inside a class conforming to ObservableObject.
  3. Final Answer:

    class Game: ObservableObject { @Published var score: Int = 0 } -> Option D
  4. Quick Check:

    Is @Published before var? Yes [OK]
Quick Trick: @Published always precedes var in property declaration [OK]
Common Mistakes:
  • Placing @Published after var
  • Omitting var keyword
  • Using 'published' instead of '@Published'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes