gpt4 book ai didi

iphone - 核心数据 TableView 部分使用 NSSortDescriptor 按工作日排序

转载 作者:行者123 更新时间:2023-12-03 19:29:07 25 4
gpt4 key购买 nike

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

唯一的问题是我不知道如何做到这一点,我的代码目前如下所示:先按字母顺序排序,然后按时间排序:

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. 为您的排序描述符创建一个自定义比较,该比较会查看“day”键并根据需要对它们进行排序。请参阅指定自定义比较 here .
  2. 将“day”键设置为整数,并按所需顺序使用枚举作为日期值。

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

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

-添加工作日的枚举:

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

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

object.day = [NSNumber numberWithInteger:kMonday];

-实现 transient “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 会针对“不支持的 NSSortDescriptor 选择器: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];

关于iphone - 核心数据 TableView 部分使用 NSSortDescriptor 按工作日排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2168141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com