translation
www.hackingwithswift.com/books/ios-s…


For more content, please follow the public account “Swift Garden”.


Like articles? How about 🔺💛➕ company 3? Follow this column, follow me 🚀🚀🚀

Build the list using @fetchRequest

Currently, we have a fetch attribute in our ContentView:

@FetchRequest(entity: Book.entity(), sortDescriptors: []) var books: FetchedResults<Book>Copy the code

We use it in a simple text view in the body:

Text("Count: \(books.count)")Copy the code

To make the interface more lively, we need to replace the text view with a list of all the books, plus their ratings and author information.

We could use the star rating control we just created, but it’s nice to try something new. Although the RatingView control is already available for any project, we can create a new EmojiRatingView that displays evaluation information specifically for this project. This example is great for showing how views in SwiftUI can be combined — how easy it is to pull a part of your view in this way.

Create a new SwiftUI view called “EmojiRatingView” as follows:

struct EmojiRatingView: View {
    let rating: Int16

    var body: some View {
        switch rating {
        case 1:
            return Text("1 ️ ⃣")
        case 2:
            return Text("2 ️ ⃣")
        case 3:
            return Text("3 ️ ⃣")
        case 4:
            return Text("4 ️ ⃣")
        default:
            return Text("1 ️ ⃣")}}}struct EmojiRatingView_Previews: PreviewProvider {
    static var previews: some View {
        EmojiRatingView(rating: 3)}}Copy the code

Note that I used Int16 for levels to make it easier to interact with Core Data. The whole code is very simple.

Now, go back to the ContentView and replace the original text view with a list created by traversing books with ForEach. For ForEach’s identifier argument, we pass \. Self, the entire object, as identifier.

You see, all the attributes in the Core Data entity are optional, so we need to use null-join operators a lot. We’ll look for alternatives later, but for now we’ll use these… Symbols.

Inside the list, we’ll use a NavigationLink that leads to a detailed view. And inside NavigationLink, we’re going to use the new EmojiRatingView, plus the title and author. Replace the text view with the following code:

List {
    ForEach(books, id: \.self) { book in
        NavigationLink(destination: Text(book.title ?? "Unknown Title")) {
            EmojiRatingView(rating: book.rating)
                .font(.largeTitle)

            VStack(alignment: .leading) {
                Text(book.title ?? "Unknown Title")
                    .font(.headline)
                Text(book.author ?? "Unknown Author")
                    .foregroundColor(.secondary)
            }
        }
    }
}Copy the code

We’ll come back to this later, but now let’s build the detail view…


My official account here Swift and computer programming related articles, as well as excellent translation of foreign articles, welcome to pay attention to ~