IOS/Objective-c: dateWithTimeInterval Issue



我不知道这是错别字还是什么,但由于某种原因,以下内容不起作用。

我想在 NSDate 中添加一秒钟。 在这样做之前,我会检查日期是 null 还是 nil,如果是,请将其设置为当前日期以避免错误。 尽管如此,在尝试添加或减去第二个后,日期显示为 null。 谁能建议出了什么问题?

int secondsAdjustment = 1;
NSDate* myLastViewed = item.lastviewed;
NSDate* myLastTouched = item.lasttouched;

if ([myLastViewed isKindOfClass:[NSNull class] ]||myLastViewed==nil) {
NSLog(@"last viewed null");
if ([myLastTouched isKindOfClass:[NSNull class]]||myLastTouched==nil) {
NSLog(@"last touched is also null so set both to now");
myLastViewed = [NSDate date];
myLastTouched = [NSDate date];
NSLog(@"my LastViewed after setting to now is:%@",myLastViewed);
NSLog(@"my LastTouched after setting to now is:%@",myLastTouched);
}
else {
NSLog(@"last touched is not null so I will set viewed to it");
myLastViewed=myLastTouched;
}
}
if (myLastViewed!=nil&&![myLastViewed isKindOfClass:[NSNull class]]) {
_lastViewedAdjusted = [NSDate dateWithTimeInterval:secondsAdjustment sinceDate:myLastViewed]; 
NSLog(@"making adjustment"); //THIS Logs  
}
else {
NSLog(@"something went wrong"); //THIS DOES NOT.  Never get here
}
NSLog(@"lastViewedAdjusted%@",_lastViewedAdjusted);//THIS IS logging as NULL

你可以用这个

NSDate *lastViewedAdjusted = [myLastViewed dateByAddingTimeInterval:1];

相关内容

  • 没有找到相关文章

最新更新