Search code examples
swiftswiftui-list

Trying to filter a List by Int greater then a value


Trying to filter a list of Books by rating > 1 and getting an error - Contextual type for closure argument list expects 1 argument, which cannot be implicitly ignored - on line List(savedFavBooks.filter({$0.rating > 1})) {

Full code below

import SwiftUI
import Combine
import SwiftData

struct SavedFavoriteBookView: View {
    
    @Query private var savedFavBooks: [Book]
    @Environment(\.modelContext) private var context
    let skyPurple = Color(red: 161 / 255, green: 160 / 255, blue: 248 / 255)
    var body: some View {
        //List{
        List(savedFavBooks.filter({$0.rating > 1})) {
            ForEach(savedFavBooks) { book in
                SavedFavBookCard(bookSavedObject: book)
                    .listRowBackground(Color(red: 255 / 255, green: 221 / 255, blue: 249 / 255) .opacity(0))
                    .navigationBarTitleDisplayMode(.inline)
            }
            .onDelete(perform: delete(indexSet:))
        }

And Book code

//  Book.swift

import Foundation
import SwiftData

struct Result: Decodable {

    let bookObjects: [Book]
}

@Model

class Book: Decodable, Identifiable, Hashable {
    let id: String
    let title: String
    let authors: [String]
    let imageURL: URL
    let subjects: [String]?
    let date_published: String
    let synopsis : String?
    var rating: Int = 0
    
    init(id: String, title: String, subjects: [String], synopsis: String, authors: [String], date_published: String, imageURL: URL, rating: Int) {
    //init(id: String, title: String, subjects: [String], authors: [String], date_published: String, imageURL: URL, rating: Int) {
        self.id = id
        self.title = title
        self.subjects = subjects
        self.authors = authors
        self.date_published = date_published
        self.synopsis = synopsis
        self.imageURL = imageURL
        self.rating = rating
    }
    
    enum CodingKeys: String, CodingKey {
        case id = "isbn"
        case title
        case date_published
        case authors
        case image
        case subjects
        case synopsis
    }
    
    required init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        date_published = try container.decode(String.self, forKey: .date_published)
        title = try container.decode(String.self, forKey: .title).trunc(length: 100)
        id = try container.decode(String.self, forKey: .id)
        authors = try container.decode(Array.self, forKey: .authors)
        subjects = try container.decodeIfPresent(Array.self, forKey: .subjects)
        synopsis = try container.decodeIfPresent(String.self, forKey: .synopsis)
        imageURL = try container.decode(URL.self, forKey: .image)
    } //end init
}

struct BookDataStore: Decodable {

//struct BookDataStore {
    var books: [Book]
}

Solution

  • Just filter Book model in your Query.

    @Query(filter: #Predicate<Book> { book in
      book.rating > 1
    }) private var savedFavBooks: [Book]