ios - 使用 NSFetchRequest 获取关系的所有结果

标签 ios objective-c nsfetchrequest

我有两个实体:作者和书籍。我想获取特定作者的所有书籍的列表。

这是我使用的代码:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Books"];
[fetchRequest setPredicate[NSPredicate predicateWithFormat:@"ANY author == %@", author]];
books = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

但是,这只会返回作者的一本书。

如何获取特定作者的所有书籍?

我阅读了很多其他主题,但似乎没有一个解决方案适合我。

谢谢!

最佳答案

你试过吗?

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Books"];
request.predicate = [NSPredicate predicateWithFormat:@"author = %@", author ];
NSArray *matches = [moc executeFetchRequest:request error:nil];

关于ios - 使用 NSFetchRequest 获取关系的所有结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23873797/

相关文章:

ios - 无法构建 Xcode 项目 : clang error: linker command failed

ios - WatchKit NSUserDefaults 为空

iphone - 核心数据故障 : how to load an entities relationship set of objects correctly

ios - 将 Google Analytics v2 更新到 v3 时出现的问题

ios - IOS 中的 ASPHTTPResquest - 响应状态代码为零

ios - 从其他应用程序获取用户事件信息

ios - NSFetchRequest 还获取我的实体类型的子类

ios - 将 AVCaptureVideoPreviewLayer 输出裁剪为正方形

iphone - 如何从数据库中检索多列数据?

ios - 为什么 NSFetchedResultsController 的 fetchedObjects 数组并不总是同质的