gpt4 book ai didi

ios - executeFetchRequest 在空核心数据上返回 nil

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:04:26 24 4
gpt4 key购买 nike

我在 Core Data 中遇到了一个奇怪的问题。基本上,我有一个空的核心数据,其中包含学生和教师等多个模型。我尝试执行以下代码:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"];
request.predicate = [NSPredicate predicateWithFormat:@"(username = %@) AND (password = %@)", username, password];

// Determines if there is a match for student
NSError *error;
NSArray *matches = [self.context executeFetchRequest:request error:&error];
if (error || !matches || [matches count] > 1) {
NSLog(@"Error in retrieving login match for student");
}
else if ([matches count]) {
student = [matches firstObject];
}

奇怪的是 matches 结果是 nil 并且错误消息被打印出来。 matches 不应该是一个空数组,因为空的核心数据只意味着上下文找不到满足这些条件的 NSManagedObject 吗?

最佳答案

简短的回答 = 是的,它应该。

根据 Apple doc :

Return Value of executeFetchRequest ... An array of objects that meet the criteria specified by request fetched from the receiver and from the persistent stores associated with the receiver’s persistent store coordinator. If an error occurs, returns nil. If no objects match the criteria specified by request, returns an empty array.

因此,检查你的 NSError

关于ios - executeFetchRequest 在空核心数据上返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25174139/

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