Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 目标C:按工作日对NSDate的NSMutableArray排序_Iphone_Objective C_Ios_Xcode_Nsdate - Fatal编程技术网

Iphone 目标C:按工作日对NSDate的NSMutableArray排序

Iphone 目标C:按工作日对NSDate的NSMutableArray排序,iphone,objective-c,ios,xcode,nsdate,Iphone,Objective C,Ios,Xcode,Nsdate,我在执行以下任务时遇到困难:我有一个NSMutableArray,其中包含从核心数据中提取的NSManagedObjects“OpeningHours” NSMutableArray *openingHoursArray = [NSMutableArray arrayWithArray: [[museum valueForKey:@"openingHours"] allObjects]]; 数组是未排序的,因为从我的NSManagedObject“museum”获取键的值会返回一个随机组织的集

我在执行以下任务时遇到困难:我有一个NSMutableArray,其中包含从核心数据中提取的NSManagedObjects“OpeningHours”

NSMutableArray *openingHoursArray = [NSMutableArray arrayWithArray: [[museum valueForKey:@"openingHours"] allObjects]];
数组是未排序的,因为从我的NSManagedObject“museum”获取键的值会返回一个随机组织的集合

数组中的NSManagedObjects“openingHours”具有NSDate类型的值,可通过键“start”和“end”访问。现在,为了以合理的方式显示我的营业时间,我访问了各自日期的工作日和时间信息,到目前为止,使用这段代码效果良好:

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:mainDelegate.localeCode];

NSDateFormatter *weekday = [[[NSDateFormatter alloc] init] autorelease];
[weekday setLocale:locale];
[weekday setDateFormat: @"EEEE"];

[NSDateFormatter *time = [[[NSDateFormatter alloc] init] autorelease];
[time setLocale:locale];
[time setTimeStyle:NSDateFormatterShortStyle];

for (NSManagedObject *openingHour in openingHoursArray) {

        NSLog(@"%@", [openingHour valueForKey:@"start"]);
        NSDate *startDate = [openingHour valueForKey:@"start"];
        NSDate *endDate = [openingHour valueForKey:@"end"];


        NSString *startDay = [weekday stringFromDate:startDate];
        NSString *endDay = [weekday stringFromDate:endDate];
        NSString *startTime = [time stringFromDate:startDate];
        NSString *endTime = [time stringFromDate:endDate];


        if (![startDay isEqualToString:endDay]) {
            [openingHoursString appendFormat:@"%@ - %@:\t%@ - %@ \n", startDay, endDay, startTime, endTime];         
        }
        else {
            [openingHoursString appendFormat:@"%@:\t%@ - %@ \n", startDay, startTime, endTime];             
        }

    }
这将为我提供所需的输出:

星期四:上午10:00至晚上9:00

星期五至星期日:上午10:00至下午6:00

星期一至星期三:上午10:00至下午5:00

现在的问题是:当然,我想先显示周一的开幕时间。我需要按开始日期值的工作日对数组进行排序。我尝试使用以下方法对其进行排序:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"start" ascending:TRUE];
[openingHoursArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
但显然这不起作用,因为它只是按日期排序,而1970-01-01是一个不幸的星期四,因此星期四将是我数组中按上述代码排序的第一项

有人对如何“解决这个问题”有好主意吗?:-)我完全被这个困住了。。
非常感谢

不要使用NSSortDescriptor,而是使用NSArray的
SortDarRayusingSelector
或NSMutableArray的
sortUsingSelector
方法,并传入
@selector(比较:)
作为选择器


有关更多信息,请参见此答案:

类似的方法应该可以:

NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSArray *sortedArray = [openingHoursArray sortedArrayUsingComparator: ^(id obj1, id obj2) {
    NSDate *startDate1 = [obj1 valueForKey:@"start"];
    NSDate *startDate2 = [obj2 valueForKey:@"start"];
    NSDateComponents *components1 = [calendar components:NSWeekdayCalendarUnit fromDate:startDate1];
    NSDateComponents *components2 = [calendar components:NSWeekdayCalendarUnit fromDate:startDate2];
    NSInteger weekday1 = [components1 weekday];
    if (weekday1 == 1) weekday1 = 8; //Order sundays last
    NSInteger weekday2 = [components2 weekday];
    if (weekday2 == 1) weekday2 = 8;
    return [[NSNumber numberWithInteger:weekday1] compare:[NSNumber numberWithInteger:weekday2]];
}];

最好是将开始日期改为2天,而不是执行日期排序,或者您是否有其他理由需要对日期进行优先排序?这是一个想法,但我不知道我需要改多少天,因为开放时间可能会有所不同-每天可能有一个单独的开放时间,或者涵盖更长的时间,比如“周一至周六”,或者基本上是其他任何时间。。我需要一个解决方案,将工作的开放时间的任何组合,始终显示星期一第一(如果星期一是开放日)。。我猜有点棘手!