SwiftData by Example: iOS 17 & SwiftUI 5 — Part 14

DevTechie
DevTechie
Published in
4 min readNov 2, 2023

--

SwiftData by Example: iOS 17 & SwiftUI 5 — Part 14

Reading Logs app is coming together fine but its missing a critical component. If user lands on first screen, they would see an empty screen.

We can improve this by providing an empty state to eliminate any confusion about data availability.

We will use newly introduced ContentUnavailableView for this scenario.

Open BookListSubview and add a Group view around List view so we can add conditional check to show the ContentUnavailableView.

import SwiftUI
import SwiftData

struct BookListSubview: View {
...

var body: some View {
Group {
List {
ForEach(books) { book in
NavigationLink(value: book) {
BookCellView(book: book)
}

}
.onDelete(perform: delete(indexSet:))
}
...

We will check books property and if its not empty, we will show the List view to render all the books.

import SwiftUI
import SwiftData

struct BookListSubview: View {
...

var body: some View {
Group {…

--

--