Xcode: Version 14.3.1 (14E300c)
iOS: 16
预览:
Code:
import SwiftUI
import CoreData
struct TodosSearch: View {
@State private var search_title = "测试"
@FetchRequest var todos_search: FetchedResults<Todo>
init() {
let request: NSFetchRequest<Todo> = Todo.fetchRequest()
request.sortDescriptors = [
NSSortDescriptor(keyPath: \Todo.title, ascending: true)
]
request.fetchLimit = 10
_todos_search = FetchRequest(fetchRequest: request)
}
var body: some View {
NavigationStack {
VStack {
TextField("Search Title", text: $search_title)
.padding()
.border(.blue)
List {
ForEach(todos_search) { todo in
if todo.title?.contains(search_title) == true {
HStack {
Text(todo.title ?? "")
}
}
}
}
.navigationTitle("Todos Search")
}
}
}
}
struct TodosSearch_Previews: PreviewProvider {
static var previews: some View {
TodosSearch()
}
}