OK,這比我最初以爲
整個系統單元標誌被用來定義日期爲元器件等困難。這個單位缺陷可以合併爲一個值,一個掩碼。我用它來做我的代碼。
年單位可能存在0....000010
月份單位可能存在0....000100
位掩碼都將
0....000010 NSCalendarUnitYear
0....000100 NSCalendarUnitMonth
------------
& 0....000110
所以保存的信息,一個單位是使用意味着我們只需要將相應的位設置爲1
- (void)viewDidLoad
{
[super viewDidLoad];
self.selectedUnitsBitmask= 0;
self.unitNames = @[ @"Year", @"Month", @"Week", @"Day", @"Hour", @"Minute", @"Second"];
self.units = @[@(NSCalendarUnitYear), @(NSCalendarUnitMonth), @(NSCalendarUnitWeekOfMonth), @(NSCalendarUnitDay), @(NSCalendarUnitHour), @(NSCalendarUnitMinute), @(NSCalendarUnitSecond)];
NSDateComponents *c = [[NSDateComponents alloc] init];
c.year = 2014;
c.month = 12;
c.day = 25;
self.futureDate = [[NSCalendar currentCalendar] dateFromComponents:c];
}
將值self.unitNames
用於填充表格視圖。
self.units
存儲每個可用單元的單位標誌。
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier [email protected]"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = _unitNames[indexPath.row];
NSUInteger s;
NSUInteger row = indexPath.row;
s = NSIntegerMax & [self.units[row] integerValue];
cell.accessoryType = (self.selectedUnitsBitmask & s) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
如果選擇的細胞,s = NSIntegerMax & [self.units[row] integerValue];
將檢查位掩碼,如果該單元將被使用。
如果選擇了一個小區,我們要切換位爲單位它代表
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger s;
NSUInteger row = indexPath.row;
s = NSIntegerMax & [self.units[row] integerValue];
self.selectedUnitsBitmask ^= s ;
NSLog(@"%lu, %lu", (unsigned long)s, (unsigned long)self.selectedUnitsBitmask);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType =(self.selectedUnitsBitmask & s) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
[self updateCountDownLabel:nil];
}
這裏
NSUInteger s;
NSUInteger row = indexPath.row;
s = NSIntegerMax & [self.units[row] integerValue];
s
表示選擇位,並
self.selectedUnitsBitmask ^= s ;
將在位掩碼中切換它。 bitmask ^= s
是bitmask = bitmask^s
的縮寫形式,其中^
是獨家或:位掩碼的第N位具有爲S
0011 bitmask
^ 0101 s
----
= 1100
的第N位相反現在我們可以更新將打印時間字符串根據位掩碼中找到的單位標誌的區別:
- (IBAction)updateCountDownLabel:(id)sender {
BOOL includeYear = self.selectedUnitsBitmask & NSCalendarUnitYear;
BOOL includeMonth = self.selectedUnitsBitmask & NSCalendarUnitMonth;
BOOL includeDay = self.selectedUnitsBitmask & NSCalendarUnitDay;
BOOL includeHour = self.selectedUnitsBitmask & NSCalendarUnitHour;
BOOL includeMinute= self.selectedUnitsBitmask & NSCalendarUnitMinute;
BOOL includeSecond= self.selectedUnitsBitmask & NSCalendarUnitSecond;
NSDateComponents *diffDateComponents = [[NSCalendar currentCalendar] components:self.selectedUnitsBitmask fromDate:[NSDate date] toDate:self.futureDate options:0];
NSMutableString *outputString = [@"" mutableCopy];
if (includeYear && diffDateComponents.year)
[outputString appendFormat:@"%d Year", diffDateComponents.year];
if (includeMonth && diffDateComponents.month)
[outputString appendFormat:@" %d Month", diffDateComponents.month];
if (diffDateComponents.weekOfMonth < NSIntegerMax && diffDateComponents.weekOfMonth)
[outputString appendFormat:@" %d Week", diffDateComponents.weekOfMonth];
if (includeDay && diffDateComponents.day)
[outputString appendFormat:@" %d Day", diffDateComponents.day];
if (includeHour && diffDateComponents.hour)
[outputString appendFormat:@" %d Hour", diffDateComponents.hour];
if (includeMinute && diffDateComponents.minute)
[outputString appendFormat:@" %d Minute", diffDateComponents.minute];
if (includeSecond && diffDateComponents.second)
[outputString appendFormat:@" %d Second", diffDateComponents.second];
self.countDownLabel.text = [outputString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
要確定標誌設置,我們我們&
AND運算
BOOL includeYear = self.selectedUnitsBitmask & NSCalendarUnitYear;
如果年份標誌0....000010
和掩碼包含0....010110
的&
操作將返回
0....000010 NSCalendarUnitYear
& 0....010110 bitmask
-------------
= 0....000010 -> NSCalendarUnitYear
我們這樣做各單位發揮的一週。我不知道爲什麼,但&操作總是返回0。下面我們用一個黑客:
if (diffDateComponents.weekOfYear < NSIntegerMax)
[outputString appendFormat:@" %d Week", diffDateComponents.weekOfYear];
新NSDateComponents week
進行實例化一個整數,NSIntegerMax最大數量表示的。如果它的值小於NSIntegerMax,我們假定它已被設置,並將其附加到字符串。
結果:
整個代碼中的一件:
#import "ViewController.h"
@interface ViewController() <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *countDownLabel;
@property (nonatomic) NSInteger selectedUnitsBitmask;
@property (strong, nonatomic) NSArray *unitNames;
@property (strong, nonatomic) NSArray *units;
@property (strong, nonatomic) NSDate *futureDate;
- (IBAction)updateCountDownLabel:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.selectedUnitsBitmask= 0;
self.unitNames = @[ @"Year", @"Month", @"Week", @"Day", @"Hour", @"Minute", @"Second"];
self.units = @[@(NSCalendarUnitYear), @(NSCalendarUnitMonth), @(NSCalendarUnitWeekOfMonth), @(NSCalendarUnitDay), @(NSCalendarUnitHour), @(NSCalendarUnitMinute), @(NSCalendarUnitSecond)];
NSDateComponents *c = [[NSDateComponents alloc] init];
c.year = 2014;
c.month = 12;
c.day = 25;
self.futureDate = [[NSCalendar currentCalendar] dateFromComponents:c];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.units count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier [email protected]"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = _unitNames[indexPath.row];
NSUInteger s;
NSUInteger row = indexPath.row;
s = NSIntegerMax & [self.units[row] integerValue];
cell.accessoryType = (self.selectedUnitsBitmask & s) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger s;
NSUInteger row = indexPath.row;
s = NSIntegerMax & [self.units[row] integerValue];
self.selectedUnitsBitmask ^= s ;
NSLog(@"%lu, %lu", (unsigned long)s, (unsigned long)self.selectedUnitsBitmask);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType =(self.selectedUnitsBitmask & s) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
[self updateCountDownLabel:nil];
}
- (IBAction)updateCountDownLabel:(id)sender {
BOOL includeYear = self.selectedUnitsBitmask & NSCalendarUnitYear;
BOOL includeMonth = self.selectedUnitsBitmask & NSCalendarUnitMonth;
BOOL includeDay = self.selectedUnitsBitmask & NSCalendarUnitDay;
BOOL includeHour = self.selectedUnitsBitmask & NSCalendarUnitHour;
BOOL includeMinute= self.selectedUnitsBitmask & NSCalendarUnitMinute;
BOOL includeSecond= self.selectedUnitsBitmask & NSCalendarUnitSecond;
NSDateComponents *diffDateComponents = [[NSCalendar currentCalendar] components:self.selectedUnitsBitmask fromDate:[NSDate date] toDate:self.futureDate options:0];
NSMutableString *outputString = [@"" mutableCopy];
if (includeYear && diffDateComponents.year)
[outputString appendFormat:@"%d Year", diffDateComponents.year];
if (includeMonth && diffDateComponents.month)
[outputString appendFormat:@" %d Month", diffDateComponents.month];
if (diffDateComponents.weekOfMonth < NSIntegerMax && diffDateComponents.weekOfMonth)
[outputString appendFormat:@" %d Week", diffDateComponents.weekOfMonth];
if (includeDay && diffDateComponents.day)
[outputString appendFormat:@" %d Day", diffDateComponents.day];
if (includeHour && diffDateComponents.hour)
[outputString appendFormat:@" %d Hour", diffDateComponents.hour];
if (includeMinute && diffDateComponents.minute)
[outputString appendFormat:@" %d Minute", diffDateComponents.minute];
if (includeSecond && diffDateComponents.second)
[outputString appendFormat:@" %d Second", diffDateComponents.second];
self.countDownLabel.text = [outputString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
我知道,我的意思是我明白,但事實並非如此,如果我用年,月,周,日,小時格式化我的日曆並顯示它,而不是我的日期,那麼將會錯過計數而不是顯示51幾個月會說3個月,因爲4年不見了。它關於建立日曆,而不是字符串 – arkmon
對不起,我不明白這個問題。也許你的問題中的一些例子會有所幫助? – ansible
我已更新我的問題 – arkmon