Browse Questions
  • Encapsulation means bundling data with the operations that act on it, and restricting direct access to that data so internal invariants can't be broken from outside.
  • URLSession is Apple's networking API for HTTP requests.
  • LLDB is the debugger built into Xcode.
  • These are SwiftUI's property wrappers for state management — each has a distinct ownership and lifecycle role.

Answer: These are SwiftUI's property wrappers for state management — each has a distinct ownership and lifecycle role.

WrapperOwns data?Source
@State✅ YesLocal to the view
@Binding❌ NoPassed in from parent
@StateObject✅ YesOwns the ObservableObject
@ObservedObject❌ NoInjected from outside
@EnvironmentObject❌ NoInjected via environment

Code Example:

// Parent creates and owns the object
struct ParentView: View {
    @StateObject private var viewModel = CounterViewModel()

    var body: some View {
        ChildView(count: $viewModel.count) // passes binding
    }
}

// Child receives a binding — does not own the data
struct ChildView: View {
    @Binding var count: Int

    var body: some View {
        Button("Increment") { count += 1 }
    }
}

Key Rule:

  • Use @StateObject when the view creates the object
  • Use @ObservedObject when the object is created elsewhere and passed in