Your Second iOS & SwiftUI App

Nov 4 2021 Swift 5.5, iOS 15, Xcode 13

Part 3: Managing Rows

23. Environment

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode
See versions

See course reviews

See forum comments
Cinema mode Mark as Complete Download course materials
Previous episode: 22. Challenge: New Book Sheet Next episode: 24. Environment Values

This video was last updated on Nov 4 2021

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

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

Coming off of that challenge, ready to go with a view that allows for creation of a book, let’s write a method that will add that new book to our library.

  var sortedBooks: [Book] { booksCache }

  func addNewBook(_ book: Book)

  /// An in-memory cache of the manually-sorted books that are persistently stored.
  func addNewBook(_ book: Book, image: Image?) {

  }
  var sortedBooks: [Book] { booksCache }

  /// <#Description#>
  /// - Parameters:
  ///   - book: <#book description#>
  ///   - image: <#image description#>
  func addNewBook(_ book: Book, image: UIImage?) {
    booksCache.insert(book, at: 0)
    uiImages[book] = image
  }
  var sortedBooks: [Book] { booksCache }

  /// <#Description#>
  func addNewBook(_ book: Book, image: UIImage?) {
  var sortedBooks: [Book] { booksCache }

  /// Adds a new book at the start of the library's manually-sorted books.
  func addNewBook(_ book: Book, image: Image?) {
  func addNewBook(_ book: Book, image: UIImage?) {
    booksCache.insert(book, at: 0)
  }
    booksCache.insert(book, at: 0)
    uiImages[book] = image
  }
mutating func addNewBook(_ book: Book, image: UIImage?) {
class Library {
  var sortedBooks: [Book] { booksCache }

  func addNewBook(_ book: Book, image: UIImage?) {
class Library: ObservableObject {
@Published private var booksCache: [Book] = [
@Published var uiImages: [Book: UIImage] = [:]
    .padding()
    .toolbar {

    }
  }
    .toolbar {
      ToolbarItem(placement: <#T##ToolbarItemPlacement#>, content: <#T##() -> _#>)
    }
    .toolbar {
      ToolbarItem(placement: .status) {

      }
    }
        ToolbarItem(placement: .status) {
          Button("Add to Library") {

          }
        }
  var body: some View {
    NavigationView {
      VStack(spacing: 24) {
      .padding()
      .navigationTitle("Got a new book?")
      .toolbar {
  @State var image: UIImage? = nil
  @ObservedObject var library: Library

  var body: some View {
@EnvironmentObject var library: Library
      trailing: Button("Add") {
        library.addNewBook(book, image: image)
      }
NewBookView().environmentObject(<#T##object: ObservableObject##ObservableObject#>)
NewBookView().environmentObject(Library())
struct ContentView: View {
  @State var addingNewBook = false
  @EnvironmentObject var library: Library

  var body: some View {
struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
      .environmentObject(Library())
      .previewedInAllColorSchemes
  }
}
    ContentView()
      .environmentObject(Library())
      .previewedInAllColorSchemes
    ContentView()
      .environmentObject(Library())
ContentView()
ContentView().environmentObject(Library())
  @ObservedObject var book: Book
  @EnvironmentObject var library: Library

  var body: some View {
  static var previews: some View {
    DetailView(book: .init())
      .environmentObject(Library())
      .previewedInAllColorSchemes
  }
ReviewAndImageStack(book: book, image: $library.images[book])
destination: DetailView(book: book)
  @ObservedObject var book: Book
  @EnvironmentObject var library: Library

  var body: some View {
uiImage: library.images[book],
        ForEach(library.sortedBooks) { book in
          BookRow(book: book)
        }