IOS/Objective-c: dateWithTimeInterval 问题
IOS/Objective-c: dateWithTimeInterval Issue
我不知道这是错字还是什么,但出于某种原因,以下内容不起作用。
我想向 NSDate 添加一秒钟。这样做之前,我检查日期是否为 null 或 nil,如果是,则将其设置为当前日期以避免错误。尽管如此,在尝试添加或减去第二个之后,日期显示为空。谁能指出哪里出了问题?
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];
我不知道这是错字还是什么,但出于某种原因,以下内容不起作用。
我想向 NSDate 添加一秒钟。这样做之前,我检查日期是否为 null 或 nil,如果是,则将其设置为当前日期以避免错误。尽管如此,在尝试添加或减去第二个之后,日期显示为空。谁能指出哪里出了问题?
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];