gpt4 book ai didi

ios - 如何使用Objective C从iOS的核心数据中过滤数据

转载 作者:行者123 更新时间:2023-12-01 18:10:12 25 4
gpt4 key购买 nike

我有一个名为的实体,学生具有以下结构:

enter image description here

我从CoreData获取数据为:

 NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
self.arrStudents = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

现在我只想要那些拥有 rollNum = 1001的学生。

我怎样才能做到这一点?

最佳答案

Try this

**Objective C**

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Student"];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"rollNum = %@", @"1001"];
[fetchRequest setPredicate:predicate];
self.arrStudents = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

**Swift**
var managedObjectContext: NSManagedObjectContext? = managedObjectContext
var fetchRequest = NSFetchRequest(entityName: "Student")
var predicate = NSPredicate(format: "rollNum = %@", "1001")
fetchRequest.predicate = predicate
arrStudents = try? managedObjectContext?.fetch(fetchRequest)

关于ios - 如何使用Objective C从iOS的核心数据中过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32923209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com