服務(wù)端返回{1,2,3,5,6}時(shí),顯示“周一至周三、周五、周六”; 服務(wù)端返回{1,2,5,6}時(shí),顯示“周一、周二、周五、周六”; 服務(wù)端返回{1,2,3,4,7}時(shí),顯示“周一至周四、周日...

#import <Foundation/Foundation.h>

//服務(wù)端返回{1,2,3,5,6}時(shí),顯示“周一至周三、周五、周六”;
//服務(wù)端返回{1,2,5,6}時(shí),顯示“周一、周二、周五、周六”;
//服務(wù)端返回{1,2,3,4,7}時(shí),顯示“周一至周四、周日”。
//服務(wù)端返回{1,2,3,4,5, 6, 7}時(shí),顯示“周一至周四、周日”。
//服務(wù)端返回{1,2,3,4,5, 6, 7}時(shí),顯示“周一至周日”。
void getConvertedWeekDayFromNumArr(NSArray<NSNumber *> *numArray) {
    static NSDictionary *dictM = nil;
    if (!dictM) {
        dictM = @{@1: @"周一",
                  @2: @"周二",
                  @3: @"周三",
                  @4: @"周四",
                  @5: @"周五",
                  @6: @"周六",
                  @7: @"周日",
        };
    }
    NSMutableArray<NSString *> *arrM = NSMutableArray.array;
    // 如周一至周三,那么`firstValue`就是周一,`lastValue`就是周三
    __block int firstValue = 0, lastValue = 0;
    // 判斷是否連續(xù),如當(dāng)前周三,那么上一個(gè)數(shù)字就是周二
    __block BOOL isSequence = NO;
    // 如周一至周三,那么`startIndex` = 周一的索引數(shù), `endIndex` = 周三的索引數(shù)
    __block NSUInteger startIndex, endIndex = NSNotFound;
    // 記錄`startIndex`和`endIndex`, 周一至周三、周五至周日,需要數(shù)組記錄兩對數(shù)字,0-2和 4-6
    NSMutableArray<NSString *> *indexArrM = NSMutableArray.array;
    [numArray enumerateObjectsUsingBlock:^(NSNumber *curNum, NSUInteger idx, BOOL *stop) {
        if (idx == 0) {
            firstValue = curNum.intValue;
        }
        int curValue = curNum.intValue;
        BOOL isBeforeSequence = isSequence,
        // 最后一個(gè)元素說明都是`周一、周二、周三、周四、周五、周六、周日`連著的也要考慮到
        isAlwaysSequence = isBeforeSequence && isSequence && idx == numArray.count - 1;
        isSequence = (curValue - lastValue == 1);
        if (isSequence && (curValue - firstValue >= 2)) {
            startIndex = [numArray indexOfObject:@(firstValue)];
            endIndex = idx;
        }
        if ((isBeforeSequence && !isSequence && startIndex != NSNotFound && endIndex != NSNotFound)
            || isAlwaysSequence) {
            [indexArrM addObject:[@[@(startIndex), @(endIndex)] componentsJoinedByString:@"-"]];
            firstValue = curValue;
        }
        [arrM addObject:dictM[curNum]];
        lastValue = curValue;
    }];
    // NSEnumerationReverse是為了數(shù)組越界
    [indexArrM enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSString *str, NSUInteger idx, BOOL * _Nonnull stop) {
        NSArray<NSString *> *indexs = [str componentsSeparatedByString:@"-"];
        startIndex = indexs.firstObject.intValue;
        endIndex = indexs.lastObject.intValue;
        NSString *startStr = arrM[startIndex], *endStr = arrM[endIndex];
        [arrM removeObjectsInRange:NSMakeRange(startIndex, endIndex - startIndex + 1)];
        [arrM insertObject:[NSString stringWithFormat:@"%@至%@", startStr, endStr] atIndex:startIndex];
    }];
    NSLog(@"arrM: %@", [arrM componentsJoinedByString:@"、"]);
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //{1,2,3,5,6}時(shí),顯示“周一至周三、周五、周六”;
        getConvertedWeekDayFromNumArr(@[@1, @2, @3, @5, @6]);
        //服務(wù)端返回{1,2,5,6}時(shí),顯示“周一、周二、周五、周六”;
        getConvertedWeekDayFromNumArr(@[@1, @2, @5, @6]);
        //服務(wù)端返回{1,2,3,4,7}時(shí),顯示“周一至周四、周日”。
        getConvertedWeekDayFromNumArr(@[@1, @2, @3, @4, @7]);
        //服務(wù)端返回{1,2,3,5, 6, 7}時(shí),顯示“周一至周三、周五至周日”。
        getConvertedWeekDayFromNumArr(@[@1, @2, @3, @5, @6, @7]);
        //服務(wù)端返回{1,2,3,4,5, 6, 7}時(shí),顯示“周一至周日”。
        getConvertedWeekDayFromNumArr(@[@1, @2, @3, @4, @5, @6, @7]);
    }
    return 0;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容