Swift SwiftUI CoreData 过滤数据 1


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()
    }
}



Entity:

Swift SwiftUI CoreData 过滤数据 1_第1张图片


喜欢或对你有帮助,点个赞吧,自己先点个嘿嘿。
有错误或者疑问还请评论指出。
我的个人网站 点击访问 hongweizhu.com。

END

你可能感兴趣的:(SwiftUI,Swift,CoreData,swift,swiftui,开发语言)