SwiftUI Fundamentals

Feb 28 2023 · Swift 5.7, macOS Venture 13.1, Xcode 14.2

Part 2: Navigation & Data Flow

16. Observable Objects

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 15. State & State Objects Next episode: 17. Challenge: StateObject & ObservableObject

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Let’s talk a bit more about ObservableObject.

struct UserInfo {
  let userName: String
  let favoriteGenre: String
}
import Combine

class UserStore: ObservableObject {
  var currentUserInfo = UserInfo(userName: "Movie Lover", favoriteGenre: .action)
}
@Published var currentUserInfo: UserInfo?
@ObservedObject var userStore = UserStore()
HStack {
  Text(userStore.currentUserInfo.userName)
  Image(systemName: "person.fill")
}
leading:
  NavigationLink(destination: UserView(userStore: userStore))
@ObservedObject var userStore: UserStore
.onAppear {
  userName = userStore.currentUserInfo.userName
  favoriteGenre = userStore.currentUserInfo.favoriteGenre 
}
static var previews: some View {
  UserView(userStore: UserStore())
}