Your Second iOS & SwiftUI App

Nov 4 2021 · Swift 5.5, iOS 15, Xcode 13

Part 3: Managing Rows

23. Environment

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: 22. Challenge: New Book Sheet Next episode: 24. Environment Values

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’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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)
        }