NSSortDescriptor 和多对多关系
我正在使用 NSFetchedResultsController
在 UITableView 中显示数据。我有一个类似于这样的多对多关系:
Article
---->>
Tag
一篇文章有很多标签。
我想对它们进行排序,以便所有标题为“Banana”的标签都位于标签为“Orange”的标签之前,而标签为“Orange”的标签又位于标签为“Apple”等的标签之前。标题是标签上的一个属性。
我正在考虑使用 NSSortDescriptor。我在我的 Tag NSManagedObject 子类中实现了一个名为“position”的方法。此方法返回一个描述标签顺序的 NSInteger。
问题是 NSSortDescriptor 不支持多对多关系。 (控制台显示:“此处不允许使用多对多键”。)这是我尝试过的:
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"tags.tagText" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {
if ([obj1 position] > [obj2 position]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([obj1 position] < [obj2 position]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
知道答案是什么吗?如何根据通过一对多关系链接的另一个实体的属性的任意值进行排序?
编辑:
我现在想知道是否可以使用 Article 类中的方法来帮助排序。我会做这样的事情:
- (NSInteger)position{
NSInteger aPosition = 100;
for (Tag *aTag in self.tags) {
if (position > [aTag position]) {
aPosition = [aTag position];
}
}
return aPosition;
}
有没有办法让我的 NSSortDescriptor 调用这个方法?
I'm working with an NSFetchedResultsController
to show data in a UITableView. I have a to many relationship which looks something like this:
Article
---->>
Tag
One article has many tags.
I'd like to order them so that all tags that have the title "Banana" come before those that have the tag "Orange" which in turn are before those tagged "Apple" etc. The title is an attribute on the tag.
I was considering using an NSSortDescriptor. I've implemented a method called "position" in my Tag NSManagedObject subclass. This method returns an NSInteger which describes the order of the tags.
The issue is that NSSortDescriptor does not support to-many relationships. (The console reads: "to-many key not allowed here".) Here's what I tried:
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"tags.tagText" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {
if ([obj1 position] > [obj2 position]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([obj1 position] < [obj2 position]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
Any idea what the answer is? How can I do a sort based on an arbitrary value of a property of another entity which is linked via a to-many relationship?
EDIT:
I'm now wondering if I can use a method in my Article class to help with the sorting. I would do something like this:
- (NSInteger)position{
NSInteger aPosition = 100;
for (Tag *aTag in self.tags) {
if (position > [aTag position]) {
aPosition = [aTag position];
}
}
return aPosition;
}
Is there a way to have my NSSortDescriptor call this method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,所以
Tag
有一个title
属性,并且您想要显示一个充满文章的表格视图,这些文章分为多个部分,其中部分是标签的标题?假设这是正确的,简短的答案是你不能。这种排序没有意义,因为一篇
Article
可以而且将会出现在排序中的多个位置。如果一篇文章有Apple和Orange标签,它应该放在哪里?Ok, so the
Tag
has atitle
attribute and you want to display a tableview full of articles that are broken into sections where the section is the title from the tag?Assuming that is correct the short answer is you can't. The sort doesn't make sense because an
Article
can and will be in more than one position in the sort. If an article has the tag Apple and Orange, where should it be placed?