基本上你有两个选择:
我写了一个包装器:
#import <Foundation/Foundation.h>
typedef NS_OPTIONS(NSUInteger, WeekDays) {
kMonday = 1 << 6,
kTuesday = 1 << 5,
kWednesday = 1 << 4,
kThursday = 1 << 3,
kFriday = 1 << 2,
kSaturday = 1 << 1,
kSunday = 1 << 0
};
@interface DaysWrapper : NSObject
-(id)initWithDays:(WeekDays)weekDays;
@end
@interface DaysWrapper ()
@property (nonatomic,retain) NSMutableArray *days;
@end
@implementation DaysWrapper{
int internalDays;
}
+(instancetype)dayWrapperWithDayBitmask:(WeekDays)daybits
{
return [[DaysWrapper alloc] initWithDays:daybits];
}
-(instancetype)initWithDays:(WeekDays)weekDays
{
if (self = [super init]) {
WeekDays wholeWeek = 127;
internalDays = weekDays & wholeWeek;
_days = [@[] mutableCopy];
while (wholeWeek) {
wholeWeek >>= 1;
if (weekDays & (wholeWeek +1)) {
[_days addObject:[NSNumber numberWithBool:YES]];
} else {
[_days addObject:[NSNumber numberWithBool:NO]];
}
}
}
return self;
}
-(NSString *)description
{
static dispatch_once_t onceToken;
static NSArray *dayNames;
static NSDictionary *fullnamesForDayNames;
dispatch_once(&onceToken, ^{
dayNames = @[@"Mon", @"Tues", @"Wend",@"Thu", @"Fri",@"Sat", @"Sun"];
fullnamesForDayNames = [[NSDictionary alloc] initWithObjects: @[@"Monday",@"Tuesday",@"Wendsday",@"Thursday",@"Friday",@"Saturday",@"Sunday"]
forKeys:dayNames];
});
NSMutableString *returnSting = [@"" mutableCopy];
__block BOOL previousWasAvailable = NO;
NSMutableArray *dayRanges = [@[] mutableCopy];
[self.days enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (!previousWasAvailable && [obj boolValue])
[dayRanges addObject:[@[] mutableCopy]];
if ([obj boolValue]) {
NSMutableArray *actualRange = [dayRanges lastObject];
[actualRange addObject:dayNames[idx]];
}
previousWasAvailable = [obj boolValue];
}];
[dayRanges enumerateObjectsUsingBlock:^(NSMutableArray *rangeArray, NSUInteger idx, BOOL *stop) {
if ([returnSting length]){
[returnSting appendString:@","];
}
if ([rangeArray count] > 1) {
[returnSting appendString: [rangeArray firstObject]];
if ([rangeArray count] > 2){
[returnSting appendString:@"-"];
} else {
[returnSting appendString:@","];
}
}
if (idx == [dayRanges count]-1){
[returnSting appendString: fullnamesForDayNames[[rangeArray lastObject]]];
} else {
[returnSting appendString:[rangeArray lastObject]];
}
}];
return returnSting;
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
WeekDays monAndFriday = kMonday | kFriday;
WeekDays thurToSat = kThursday | kFriday | kSaturday;
WeekDays weekEnd = kFriday | kSaturday | kSunday;
WeekDays mondAndWeekend = weekEnd | kMonday;
WeekDays friSat = kFriday | kSaturday;
WeekDays everySecondDay = 8 | 32 | 2;
WeekDays moday = kMonday;
WeekDays workWeek = kMonday | kTuesday | kWednesday | kThursday | kFriday;
WeekDays wholeWeek = workWeek | weekEnd;
WeekDays notThursday = ~kThursday & wholeWeek;
NSLog(@"%@", [DaysWrapper dayWrapperWithDayBitmask:monAndFriday]);
NSLog(@"%@", [DaysWrapper dayWrapperWithDayBitmask:weekEnd]);
NSLog(@"%@", [DaysWrapper dayWrapperWithDayBitmask:thurToSat]);
NSLog(@"%@", [DaysWrapper dayWrapperWithDayBitmask:mondAndWeekend]);
NSLog(@"%@", [DaysWrapper dayWrapperWithDayBitmask:friSat]);
NSLog(@"%@", [DaysWrapper dayWrapperWithDayBitmask:everySecondDay]);
NSLog(@"%@", [DaysWrapper dayWrapperWithDayBitmask:moday]);
NSLog(@"%@", [DaysWrapper dayWrapperWithDayBitmask:workWeek]);
NSLog(@"%@", [DaysWrapper dayWrapperWithDayBitmask:wholeWeek]);
NSLog(@"%@", [DaysWrapper dayWrapperWithDayBitmask:notThursday]);
}
return 0;
}
输出
Mon,Friday
Fri-Sunday
Thu-Saturday
Mon,Fri-Sunday
Fri,Saturday
Tues,Thu,Saturday
Monday
Mon-Friday
Mon-Sunday
Mon-Wend,Fri-Sunday
随意的想法
- 而不是在数组名称中保存整数信息的副本
self.days
,您可以在每次需要时动态处理它以减少空间
- 工厂基本上具有相同的逻辑
通用代码大修(ARC、现代 obj-c 语法、NS_OPTIONS、……)2015 年 5 月 21 日