排序描述符中的函数
在我的核心数据模型中,我有一个具有firstName 和lastName 属性的人员实体。当我将排序描述符与 fetchedResultsController 一起使用时,我按名字排序。但如果firstName为零,我想用lastName代替。
我在人和对象之间也有一种关系,称为 personOfObjects,其中 person<-->>objects。我正在排序以呈现在 UITableView 中。除了名字为零的情况外,它工作正常。在这种情况下,它们都会被分到同一组中,而这些特殊情况实际上应该按姓氏排序。
这就是我现在所做的:
NSEntityDescription * entity = [NSEntityDescription entityForName:@"object" inManagedObjectContext:dataInterface.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *personSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"personOfObject.firstName" ascending:YES];
NSSortDescriptor *objectSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"objectDescription" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:personSortDescriptor, objectSortDescriptor, nil];
我想我可以在 person 类中创建一个辅助函数,如下所示:
- (NSString *) firstLast {
if (![self.firstName isNullString] ) return self.firstName;
if (![self.lastName isNullString] ) return self.lastName;
return @"";
}
然后更改第一个排序描述符以引用此函数:
NSSortDescriptor *personSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"personOfObject.firstLast" ascending:YES];
NSSortDescriptor *objectSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"objectDescription" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:personSortDescriptor, objectSortDescriptor, nil];
我真的没想到它会起作用(而且它没有' t),但我想知道是否有一种简单的方法可以做这样的事情。
In my core data model, I have a person entity with properties of firstName and lastName. When I use sort descriptors with a fetchedResultsController, I sort by firstName. But if the firstName is nil, I want to use the lastName instead.
I also have a relationship between person and objects, called personOfObjects where person<-->>objects. I am sorting to present in a UITableView. It works fine except for the cases where the first name is nil. In that case they all get grouped into the same group, when these special cases should really be sorted by the last name instead.
Here is what I do now:
NSEntityDescription * entity = [NSEntityDescription entityForName:@"object" inManagedObjectContext:dataInterface.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *personSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"personOfObject.firstName" ascending:YES];
NSSortDescriptor *objectSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"objectDescription" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:personSortDescriptor, objectSortDescriptor, nil];
I thought I could create a helper function in the person class, like this:
- (NSString *) firstLast {
if (![self.firstName isNullString] ) return self.firstName;
if (![self.lastName isNullString] ) return self.lastName;
return @"";
}
and then change the first sort descriptor to refer to this function:
NSSortDescriptor *personSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"personOfObject.firstLast" ascending:YES];
NSSortDescriptor *objectSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"objectDescription" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:personSortDescriptor, objectSortDescriptor, nil];
I didn't really expect this to work (and it didn't), but I am wondering if there is a straightforward way to do something like this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用自己的 NSComparator 来完成此操作( How to use NSComparator? )
这个问题有一些代码:
如何通过对象图对托管对象的 NSMutableArray 进行排序
您必须在比较器中添加条件比较,而不是链接排序描述符。
You can do this by using your own NSComparator ( How to use NSComparator? )
this question has some code for it, too:
How to Sort an NSMutableArray of Managed Objects through an object graph
You'll have to add your conditional compare in the comparator rather than chaining sort descriptors.