2

我的程序从用户输入接收到的天数数组(字符串格式),长度在 1-7 之间。接收到的数组具有全文的日期名称。

我可能收到的一个数组示例是:["Tuesday", "Monday", "Thursday"]

我要做的是对这个数组进行排序Monday to Sunday并将全文名称转换为缩写。因此,我对上述数组的排序函数理想情况下会返回:["M", "Tu", "Th"]

永远不会出现同一天的重复项,永远不会少于 1 个,永远不会超过 7 个。

谢谢。

这很粗糙,但这是用户从中选择天数的粗略 UI:

在此处输入图像描述

我使用了选定的答案,但对其进行了调整以将其添加到我需要的一个地方。我对其进行了如下调整:

-(NSArray*)array:(NSArray*)array collect:(id(^)(id object))block
{
    NSMutableArray * result = [ NSMutableArray array ] ;
    for( id object in array ) { [ result addObject:block( object) ] ; }
    return result ;
}

-(NSArray*)arrayBySortingAndAbbreviatingDayNames:(NSArray*)arrayToSort
{
    NSArray * dayNames = @[ @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday",     @"Saturday", @"Sunday" ] ;
    NSArray * abbreviations = @[ @"M", @"Tu", @"W", @"Th", @"F", @"Sa", @"Su" ] ;
    NSArray * array = [ self array:arrayToSort collect:^(NSString * dayName){
        return @([ dayNames indexOfObject:dayName ]) ;
    } ] ;
    array = [ array sortedArrayUsingSelector:@selector( compare: ) ] ;
    array = [ self array:array collect:^(NSNumber * index){
        return abbreviations[ [ index integerValue ] ] ;
    }];
    return array ;
}
4

2 回答 2

3

这是做同样事情的另一种方法。

NSArray *input = @[@"Tuesday",@"Friday",@"Wednesday",@"Saturday"];

NSArray *dayArray = @[@{@"Monday":@"M"},@{@"Tuesday":@"Tu"},@{@"Wednesday":@"W"},@{@"Thursday":@"Th"},@{@"Friday":@"F"},@{@"Saturday":@"Sa"},@{@"Sunday":@"Su"}];
NSMutableArray *output = [@[@"",@"",@"",@"",@"",@"",@""] mutableCopy];
for (NSString *aDay in input) {
    NSInteger indx = [dayArray indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        return [[dayArray[idx] allKeys][0] isEqualToString:aDay];
    }];
    [output replaceObjectAtIndex:indx withObject:dayArray[indx][aDay]];
}
[output removeObjectIdenticalTo:@""];
NSLog(@"%@",output);
于 2013-05-14T01:51:02.827 回答
3
@implementation NSArray (DayNameThing)

-(NSArray*)collect:(id(^)(id object))block
{
    NSMutableArray * result = [ NSMutableArray array ] ;
    for( id object in self ) { [ result addObject:block( object) ] ; }
    return result ;
}

-(NSArray*)arrayBySortingAndAbbreviatingDayNames
{
    NSArray * dayNames = @[ @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", @"Sunday" ] ;
    NSArray * abbreviations = @[ @"M", @"Tu", @"W", @"Th", @"F", @"Sa", @"Su" ] ;
    NSArray * array = [ self collect:^(NSString * dayName){
        return @([ dayNames indexOfObject:dayName ]) ;
    } ] ;
    array = [ array sortedArrayUsingSelector:@selector( compare: ) ] ;
    array = [ array collect:^(NSNumber * index){
        return abbreviations[ [ index integerValue ] ] ;
    }];
    return array ;
}

@end

所以...如果array有用户选择的日期名称,你可以得到你想要的结果newArray = [ array arrayBySortingAndAbbreviatingDayNames ]..


编辑想到一个更好/更简单的方法:

NSArray * convertArray(NSArray * input)
{
    NSArray * dayNames = @[ @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", @"Sunday" ] ;
    NSArray * shortDayNames = @[ @"M", @"Tu", @"W", @"Th", @"F", @"Sa", @"Su" ] ;
    NSMutableArray * output = [ NSMutableArray array ] ;
    for( int index=0; index < 7; ++index )
    {
        if ( [ input containsObject:dayNames[ index ]] )
        {
            [ output addObject:shortDayNames[ index ]] ;
        }
    }
    return output ; 
}

编辑得更好。

NSArray * ConvertArray(NSArray * input)
{
    NSArray * dayNames = @[ @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", @"Sunday" ] ;
    NSArray * shortDayNames = @[ @"M", @"Tu", @"W", @"Th", @"F", @"Sa", @"Su" ] ;

    return [ shortDayNames objectsAtIndexes:[ dayNames indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        return [ input containsObject:obj ] ;
    }]] ;
}

...好吧,我想我已经完成了。:)

于 2013-05-14T01:22:43.463 回答