Iphone 混淆日期比较

Iphone 混淆日期比较,iphone,objective-c,Iphone,Objective C,我对这段代码感到困惑: for (int i = 0; i < [content count]; i++) { NSString *dateString = [self replaceCharacters:[[[content objectAtIndex:i] objectForKey:@"date"] objectForKey:@"text"]]; NSLog(@"\n\n%i",[[dateFormatter stringFromDate:[originalDate d

我对这段代码感到困惑:

for (int i = 0; i < [content count]; i++) {
    NSString *dateString = [self replaceCharacters:[[[content objectAtIndex:i] objectForKey:@"date"] objectForKey:@"text"]];
    NSLog(@"\n\n%i",[[dateFormatter stringFromDate:[originalDate dateFromString:[[[content objectAtIndex:i] objectForKey:@"date"] objectForKey:@"text"]]] intValue]);
    if ([[dateFormatter stringFromDate:[originalDate dateFromString:dateString]] intValue] >= [todayString intValue]) {
        [tempContent addObject:[content objectAtIndex:i]];
    }

}
for(int i=0;i<[content count];i++){
NSString*dateString=[self-replaceCharacters:[[content-objectAtIndex:i]objectForKey:@“date”]objectForKey:@“text”];
NSLog(@“\n\n%i”,[[dateFormatter stringFromDate:[originalDate dateFromString:[[content-objectAtIndex:i]objectForKey:@“date”]objectForKey:@“text”]]intValue]);
如果([[dateFormatter stringFromDate:[OriginalDateFromString:dateString]]intValue]>=[todayString intValue]){
[tempContent addObject:[content objectAtIndex:i]];
}
}
我会解释:

我需要检查内容中每个对象的日期是否高于或等于今天的日期。如果是这样,它将被添加到内容中。但是tempContent计数等于content计数。什么都没安排好

有人看到解决办法了吗

谢谢,
mavrick3.

您希望使用NSDateComponents,而不是所有stringFromDate的东西

有关更多信息,请参阅

但基本上你会想要这样的东西:

unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* origComponents = [calendar components:flags fromDate:originalDate];
NSDate* origDateOnly = [calendar dateFromComponents:origComponents];

for (int i = 0; i < [content count]; i++) 
{
    NSDate *date = [[content objectAtIndex:i] objectForKey:@"date"];
    NSDateComponents* components = [calendar components:flags fromDate:date];
    NSDate* dateOnly = [calendar dateFromComponents:components];

    if ([origDateOnly compare:dateOnly] == (NSOrderedSame || NSOrderedAscending)) 
    {
         [tempContent addObject:[content objectAtIndex:i]];
    }
}
unsigned int flags=NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar*日历=[NSCalendar currentCalendar];
NSDateComponents*origComponents=[日历组件:flags fromDate:originalDate];
NSDate*origDateOnly=[日历日期来自组件:origComponents];
对于(int i=0;i<[内容计数];i++)
{
NSDate*date=[[content-objectAtIndex:i]objectForKey:@“date”];
NSDateComponents*components=[calendar components:flags fromDate:date];
NSDate*dateOnly=[calendar dateFromComponents:components];
if([origDateOnly compare:dateOnly]==(SensorDeredName | | SensorDeredAscing))
{
[tempContent addObject:[content objectAtIndex:i]];
}
}

您希望使用NSDATE组件,而不是所有stringFromDate内容

有关更多信息,请参阅

但基本上你会想要这样的东西:

unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* origComponents = [calendar components:flags fromDate:originalDate];
NSDate* origDateOnly = [calendar dateFromComponents:origComponents];

for (int i = 0; i < [content count]; i++) 
{
    NSDate *date = [[content objectAtIndex:i] objectForKey:@"date"];
    NSDateComponents* components = [calendar components:flags fromDate:date];
    NSDate* dateOnly = [calendar dateFromComponents:components];

    if ([origDateOnly compare:dateOnly] == (NSOrderedSame || NSOrderedAscending)) 
    {
         [tempContent addObject:[content objectAtIndex:i]];
    }
}
unsigned int flags=NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar*日历=[NSCalendar currentCalendar];
NSDateComponents*origComponents=[日历组件:flags fromDate:originalDate];
NSDate*origDateOnly=[日历日期来自组件:origComponents];
对于(int i=0;i<[内容计数];i++)
{
NSDate*date=[[content-objectAtIndex:i]objectForKey:@“date”];
NSDateComponents*components=[calendar components:flags fromDate:date];
NSDate*dateOnly=[calendar dateFromComponents:components];
if([origDateOnly compare:dateOnly]==(SensorDeredName | | SensorDeredAscing))
{
[tempContent addObject:[content objectAtIndex:i]];
}
}

看看Erica Sadun的NSDate扩展。它有很多有用的方法,比如
IsequalDateIgnoringTime:
isLaterThanDate:


这帮助我解决了与您类似的问题。

看看Erica Sadun的NSDate扩展。它有很多有用的方法,比如
IsequalDateIgnoringTime:
isLaterThanDate:


这帮助我解决了与您类似的问题。

为什么不使用
NSDate
s
-比较:
方法?如果第一个日期在时间上较晚,则返回常量
传感器终止搜索
,如果第一个日期较早,则返回常量
传感器终止搜索
,当返回
传感器终止搜索
时,您将发现自己

例如:

if ([originalDate compare:todayDate] == NSOrderedDescending) {
        [tempContent addObject:[content objectAtIndex:i]];
}

为什么不使用
NSDate
s
-compare:
方法?如果第一个日期在时间上较晚,则返回常量
传感器终止搜索
,如果第一个日期较早,则返回常量
传感器终止搜索
,当返回
传感器终止搜索
时,您将发现自己

例如:

if ([originalDate compare:todayDate] == NSOrderedDescending) {
        [tempContent addObject:[content objectAtIndex:i]];
}

那么,为了澄清,您想将未来的日期添加到内容中吗?您是将时间包括在比较中,还是仅包括日期?因此,为了澄清,您希望将未来的日期添加到内容中?你是在比较中包括时间,还是只包括日期?如果我使用这个,我会得到一些错误。。。我知道我需要添加一些东西。。。但是整个代码都有缺陷……我修复了我在代码中发现的一个缺陷,但由于您在原始代码中没有提供太多信息(如“内容”对象的结构),我只是在浏览器中快速键入了一些内容,并没有真正测试它。其主要思想是使用日期组件而不是日期->字符串->整数转换来进行比较,这样更清晰、更容易理解。。。我知道我需要添加一些东西。。。但是整个代码都有缺陷……我修复了我在代码中发现的一个缺陷,但由于您在原始代码中没有提供太多信息(如“内容”对象的结构),我只是在浏览器中快速键入了一些内容,并没有真正测试它。其主要思想是使用日期组件而不是日期->字符串->整数转换来进行比较更简洁、更容易理解但它不适用于与今天日期相等的日期…
|
是按位or运算符。如果这样写,则将其应用于常量,而不是比较结果。尝试
if([[originalDate dateFromString:dateString]compare:today]==sensorderedescending | |[originalDate dateFromString:dateString]compare:today]==sensorderedsame){[tempContent addObject:[content objectAtIndex:i]];
我试着这样记住它:如果你有
[A compare:B]
,试着想象一下,这是一个由两个整数组成的序列,从左到右读取。如果
A=5
B=3
,则序列为
53
;因此,这个序列被称为降序(
sensorderedescending