检查 NSFetchedResultsController 是否不返回任何对象
我想检查 NSFetchedResultsController 是否未返回任何对象。这样做的重点是我想有条件地在页面上显示一些信息,以通知用户没有对象。 (并且还给他们一个创建一个的选项。)
我将其连接到一个普通的表视图。我的想法是,我可以在 if 语句中检查这一点,如下所示:
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
if ([[[_fetchedResultsController sections] objectAtIndex:section] numberOfObjects] == 0) {
return @"Message goes here.";
}
return nil;
}
这工作正常,但我不确定这是否是解决此问题的最佳方法。另外,该方法将部分信息作为参数,因此我仅限于在某些区域使用它。如果我在获取请求中没有使用sectionNameKeyPath,我可以这样做:
if ([[[_fetchedResultsController sections] objectAtIndex:0] numberOfObjects] == 0)
并且只使用0代替section吗?我已经查看了这篇文章,但答案似乎并没有真正回答的问题。
I want to check if a NSFetchedResultsController isn't returning any objects. The point of this is that I want to conditionally display some information on the page to inform the user that there are no objects. (And also give them an option to create one.)
I have this connected to a normal table view. My thought was that I could check this in an if statement like this:
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
if ([[[_fetchedResultsController sections] objectAtIndex:section] numberOfObjects] == 0) {
return @"Message goes here.";
}
return nil;
}
This works fine, but I'm not sure if it is the best way to go about this. Also, the method is taking the section info as an argument, so I'm kind of restricted to using this only in certain areas. If I am not using a sectionNameKeyPath in my fetch request, would I be able to do this:
if ([[[_fetchedResultsController sections] objectAtIndex:0] numberOfObjects] == 0)
and just use 0 in place of section? I've check out this post, but the answer doesn't really seem to answer the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您主要感兴趣的是是否返回零个对象(相对于超过零个对象),那么 -[NSFetchedResultsController fetchedObjects] 看起来它将提供信息(假设已调用 PerformFetch)。
If your main interest is in whether zero objects are returned (vs. more than zero), then -[NSFetchedResultsController fetchedObjects] looks like it will provide the information (provided performFetch has been called).