I'm trying to search through a list of bookmarks by name stored in Core Data but I'm getting this error next to 'return items' in var searchResults
"Cannot convert return expression of type 'FetchedResults <MyBookmark to return type '[String]'"
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \MyBookmark.name, ascending: true)], animation: .default)
private var items: FetchedResults<MyBookmark>
@State private var searchText = ""
ForEach(searchResults, id: \.self) { myBookmark in
Text(myBookmark.name!)
Text(myBookmark.url!)
}
.searchable(text: $searchText)
var searchResults: [String] {
if searchText.isEmpty {
return items
} else {
return items.filter { $0.localizedCaseInsensitiveContains(searchText) }
}
}
MyBookmark
@objc(MyBookmark)
public class MyBookmark: NSManagedObject, BaseModel {
static var all: NSFetchRequest<MyBookmark> {
let request: NSFetchRequest<MyBookmark> = MyBookmark.fetchRequest()
request.sortDescriptors = []
return request
}
}
extension MyBookmark {
@nonobjc public class func fetchRequest() -> NSFetchRequest<MyBookmark> {
return NSFetchRequest<MyBookmark>(entityName: "MyBookmark")
}
@NSManaged public var name: String?
@NSManaged public var url: String?
}
extension MyBookmark : Identifiable {
}
This is for a macOS app
CodePudding user response:
To be able to use MyBookmark
in your ForEach
properly the search functionality should also work with MyBookmark
So change the search result array to use MyBookmark
instead
var searchResults: [MyBookmark] {
if searchText.isEmpty {
return items
} else {
return items.filter {
$0.name.localizedCaseInsensitiveContains(searchText) }
}
}
}
CodePudding user response:
That var must return an array of String
, but you are returning the array items
, which is an array of type MyBookmark
.
Solution: convert the array to a type of [String]
.
Replace that variable with:
var searchResults: [String] {
if searchText.isEmpty {
return items.compactMap { $0.name }
} else {
return items.compactMap { $0.name }.filter { $0.localizedCaseInsensitiveContains(searchText) }
}
}