无法转换类型的返回表达式' fetchedResults<>'返回类型' [string]'
我正在尝试通过存储在核心数据中的名称的书签列表中进行搜索,但是我在var SearchResults中的“返回项目”旁边遇到此错误
“无法将'fetty'fetchedResults< mybookmark的返回表达式转换为返回类型'[字符串]”“
@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 {
}
这是Macos应用程序
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当搜索过滤器更改时,您需要再次执行提取请求。为此,您需要设置获取请求的谓词。
添加一个新属性来处理此问题
然后将
.searchable
修饰符与该属性一起使用You need to execute your fetch request again when the search filter changes. To do that you need to set the predicate of the fetch request.
Add a new property to handle this
And then use the
.searchable
modifier together with this property该 var 必须返回一个
String
数组,但您返回的是items
数组,它是一个MyBookmark
类型的数组。解决方案:将数组转换为
[String]
类型。将该变量替换为:
That var must return an array of
String
, but you are returning the arrayitems
, which is an array of typeMyBookmark
.Solution: convert the array to a type of
[String]
.Replace that variable with: