Ios 将NSDateComponents与整数进行比较(或将NSDateComponents转换为整数)
我正在检查应用程序安装是否经过X天来解锁某些功能Ios 将NSDateComponents与整数进行比较(或将NSDateComponents转换为整数),ios,objective-c,nsdate,nsdatecomponents,Ios,Objective C,Nsdate,Nsdatecomponents,我正在检查应用程序安装是否经过X天来解锁某些功能 NSDate *now = [NSDate date]; NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitDay
NSDate *now = [NSDate date];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitDay
fromDate:[[NSUserDefaults standardUserDefaults]
objectForKey:@"firstTimeDate"]
toDate:now
options:0];
但当我这么做的时候
if (!(components < 1)) {
...
}
if(!(组件<1)){
...
}
我明白了
指针和整数之间的有序比较('NSDateComponents*'和'int')
由于您需要天数,因此应该访问
day
属性
if (components.day >= 1) {
}
注意:使用
x>=y
可能比使用更清晰!(x
不错!天数在00:00:00或每24小时递增?尝试不同的值,但我相信如果两个日期相隔24小时,您将得到1天。