如何限制 SwiftUI 中 FetchRequest 的结果数量

How to limit the number of results in a FetchRequest in SwiftUI

在向 CoreData 生成 FetchRequest 时如何限制检索到的 FetchedResults 的大小?

struct ContentView: View {
    var fetchRequest:FetchRequest<Kana>

    init(){
        fetchRequest = FetchRequest<Kana>(entity: Kana.entity(), sortDescriptors: [], predicate: NSPredicate(format: "alphabet == %@", "hiragana"))
    }

    var body: some View {
        List{
            Text("10 Kanas")
            // Display 10 Kanas
        }
    }
}

您需要使用相应的初始化程序创建 FetchRequestNSFeatchRequest,如下所示

init() {
    let request: NSFetchRequest<Kana> = Kana.fetchRequest()
    request.fetchLimit = 10
    request.predicate = NSPredicate(format: "alphabet == %@", "hiragana")
    fetchRequest = FetchRequest<Kana>(fetchRequest: request)
}

backup