核心数据表视图部分按工作日使用NSSortDescriptor进

核心数据表视图部分按工作日使用NSSortDescriptor进

本文介绍了核心数据表视图部分按工作日使用NSSortDescriptor进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试将我的对象数组排序为天顺序,这样他们可以正确的顺序,即星期一,星期二,星期三,然后按开始时间分组。

I'm currently trying to sort my array of objects into day order so they can be grouped in the correct order i.e. Monday, Tuesday, Wednesday then by start time.

只有问题是我不知道如何做到这一点,我的代码目前看起来像这样:按字母顺序按时间排序:

Only problem is I can't figure out how to do this, my code currently looks like this: Which sorts alphabetically then by time:

NSString *sectionKey = nil;
switch (tab) {

case kByWeekA: {
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"day" ascending:NO];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"starttime" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptor1 release];
[sortDescriptor2 release];
[sortDescriptors release];
sectionKey = @"day";
break;
}
case kByWeekB:{
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"day" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"starttime" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptor1 release];
[sortDescriptor2 release];
[sortDescriptors release];
sectionKey = @"day";
break;
}
default:
break;
}

请帮助!

推荐答案

我看到两个解决方案:


  1. 为您的排序描述符创建自定义比较, 天键,并根据需要对其进行排序。请参阅指定自定义比较。

  2. 将日键设置为整数,并按照所需顺序对日值进行枚举。

UPDATE 2 选项2的示例代码:

-Change将day属性赋予一个数字(Int16),并添加一个暂时(非持久)dayName字符串属性。

-Change the "day" attribute to a number (Int16) and add a transient (non-persistant) "dayName" string attribute.

typdef enum {
    kMonday,
    kTuesday,
    kWednesday,
    kThursday,
    kFriday,
    kSaturday,
    kSunday
} WeekDay;

将托管对象的day属性设置为NSNumber对象。例如:

-Set the "day" property of your managed objects to be NSNumber objects. For example:

object.day = [NSNumber numberWithInteger:kMonday];

- 为瞬态dayName属性实现getter函数:

-Implement the getter function for the transient "dayName" property:

- (NSString*)dayName {
    switch ((WeekDay)self.day.integerValue) {
        case kMonday:
            return @"Monday";
            break;
        case kTuesday:
            return @"Tuesday";
            break;
        case kWednesday:
            return @"Wednesday";
            break;
        case kMonday:
            return @"Monday";
            break;
        case kThursday:
            return @"Thursday";
            break;
        case kFriday:
            return @"Friday";
            break;
        case kSaturday:
            return @"Saturday";
            break;
        case kSunday:
            return @"Sunday";
            break;
    }
}

在获取请求中,并使用dayName作为节名称键路径。

-In the fetch request, sort by "day" and use "dayName" as the section name key path.

UPDATE

注意:但是Core Data为'不支持的NSSortDescriptor选择器:weekdayCompare:'引发了一个'NSInvalidArgumentException'异常

UPDATE
Note: Option 1 works fine for sorting an array, but Core Data is throwing an 'NSInvalidArgumentException' exception for 'unsupported NSSortDescriptor selector: weekdayCompare:'

选项1的示例代码:

// category on NSString for custom comparison
@interface NSString (WeekdayComparison)
- (NSComparisonResult)weekdayCompare:(NSString*)otherDay;
@end
@implementation NSString (WeekdayComparison)
- (NSComparisonResult)weekdayCompare:(NSString*)otherDay {
    NSArray *weekDays = [NSArray arrayWithObjects:@"Sunday", @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", nil];

    NSUInteger selfIndex = [weekDays indexOfObject:self];
    NSUInteger otherDayIndex = [weekDays indexOfObject:otherDay];

    if (selfIndex < otherDayIndex) {
        return NSOrderedAscending;
    }
    else if (selfIndex > otherDayIndex) {
        return NSOrderedDescending;
    } else {
        return NSOrderedSame;
    }
}
@end

方法:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"day" ascending:YES selector:@selector(weekdayCompare:)];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[sortDescriptor release];

这篇关于核心数据表视图部分按工作日使用NSSortDescriptor进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 17:24