3

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

唯一的问题是我无法弄清楚如何做到这一点,我的代码目前看起来像这样: 按字母顺序然后按时间排序:

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;
}

请帮忙!

4

2 回答 2

13

我看到两个解决方案:

  1. 为您的排序描述符创建一个自定义比较,查看“day”键并根据需要对其进行排序。请参阅此处指定自定义比较。
  2. 将“day”键设为整数,并按所需顺序对日期值使用枚举。

更新 2选项 2 的示例代码:

-将“day”属性更改为数字(Int16)并添加瞬态(非持久)“dayName”字符串属性。

- 添加工作日的枚举:

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

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

object.day = [NSNumber numberWithInteger:kMonday];

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

- (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;
    }
}

- 在获取请求中,按“day”排序,并使用“dayName”作为节名键路径。

更新
注意:选项 1 适用于对数组进行排序,但 Core Data 为 'unsupported NSSortDescriptor selector: weekdayCompare:' 抛出了 'NSInvalidArgumentException' 异常

选项 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];
于 2010-01-30T20:33:43.283 回答
0

我正在处理对部分的自定义排序的相同要求,解决方案#1对我来说非常有效,谢谢!

我想补充的一件事是,如果您的应用程序在加载几秒钟后神秘地崩溃,则在添加瞬态属性后,您需要重置数据库。

NSError *error;
NSURL *applicationDocumentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *databaseURL = [applicationDocumentsDirectory URLByAppendingPathComponent:@"My Database Name"];
[[NSFileManager defaultManager] removeItemAtPath:databaseURL.path error:&error];

希望这可以帮助!

于 2012-11-10T21:54:19.860 回答