基本的に、次の 2 つのオプションがあります。
私はラッパーを書きました:
#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
ランダムな考え
- 整数の情報のコピーを配列 name に保持する代わりに、
self.days
スペースを削減するために必要になるたびにその場で処理できます
- 工場は基本的に同じロジックを持っています
一般的なコードのオーバーホール (ARC、最新の obj-c 構文、NS_OPTIONS など) 2015 年 5 月 21 日