Sunday, June 2, 2024
HomeiOS Developmentios - Search inside array of customized objects inside dictionary

ios – Search inside array of customized objects inside dictionary


I’ve a dictionary the place secret’s of kind string and worth is an array of customized objects. I’m engaged on search UI, the place when consumer searches on one thing, app ought to test for search time period inside one of many properties of that customized object. Nevertheless, I get some error.

import SwiftUI

struct E book: Hashable {
    var bookName: String
    var authorName: String
}

struct ContentView: View {
    let bookData: [String: [Book]] = ["Fictional": [Book.init(bookName: "titleA", authorName: "AuthorA"), Book.init(bookName: "titleB", authorName: "AuthorB")],
                                      "Non-Fictional":  [Book.init(bookName: "titleCA", authorName: "AuthorC")]]
    
    var physique: some View {
        VStack {
            Textual content("Howdy World")
        }
        .onAppear {
            let matchingResults = bookData.filter({ $0.worth.filter({ $0.bookName.localizedCaseInsensitiveContains("A") }) })
            
            // Anticipating under output since e book names have 'A' in it.
            // ["Fictional": [Book.init(bookName: "titleA", authorName: "AuthorA")], "Non-Fictional":  [Book.init(bookName: "titleCA", authorName: "AuthorC")]]
        }
    }
}

Within the above instance, if I seek for ‘A’ or ‘a’, the output must be
["Fictional": [Book.init(bookName: "titleA", authorName: "AuthorA")], "Non-Fictional": [Book.init(bookName: "titleCA", authorName: "AuthorC")]] since “titleA” and “titleCA” comprises A.

However as an alternative I’m seeing following error "Can't convert worth of kind '[Employee]' to closure end result kind 'Bool'" on line the place I’m filtering the info i.e. "searchResults = groupedEmployees.filter({ $0.worth.filter({ $0.fullName.localizedCaseInsensitiveContains(searchTerm) }) })"

How do I search inside array of customized objects in a dictionary?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments