0

我有一些NSDates是递增和循环的。

在循环中,我需要检查isEqualToDate数组中的当前日期。

然后每个日期键需要有多个与之关联的字符串值,就像 PHP 中的这样

$array['date'][0]['string']
$array['date'][1]['string']
etc

我读了一点,我知道Objective-C不支持这些类型的数组,最好的方法是什么?

4

2 回答 2

0

容纳一切的可变字典

NSMutableDictionary *myDates = [[NSMutableDictionary alloc] init];

向字典中添加值

[myDates setObject:myArrayOfStrings forKey:myDate];

比较日期而不比较时间(小时/分钟/秒)。
信用到期的信用:iOS:比较两个没有时间部分的 NSDate-s

NSCalendar *calendar = [NSCalendar currentCalendar]; 
NSInteger comps = (NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit);

NSDateComponents *date1Components = [calendar components:comps 
                                            fromDate: date1];
NSDateComponents *date2Components = [calendar components:comps 
                                            fromDate: date2];

date1 = [calendar dateFromComponents:date1Components];
date2 = [calendar dateFromComponents:date2Components];

NSComparisonResult result = [date1 compare:date2];
if (result == NSOrderedAscending) {
} else if (result == NSOrderedDescending) {
}  else {
    //the same
}
于 2013-07-30T04:18:22.173 回答
0

在 Objective-C 中,这就像一个带有日期键的NSArrayinto 。例子:-NSDictionary

NSDictionary *dict = @{@"date":@[@"date-1", @"date-2"]};

输出是:

date = (
    "date-1",
    "date-2"
);

您可以获取这些日期,例如:

NSString *dateString = [[dict objectForKey:@"date"] objectAtIndex:0];

或者

// This is new syntax of dictionary and array
NSString *dateString = dict[@"date"][0];

它会给你0索引的日期。这是关于数组类型的,现在请参阅如何检查日期是否存在于数组中。如果您的日期是NSString类型,那么您可以简单地检查

if ([[dic objectForKey:@"date"] containsObject:@"date-1"])
{
    // date-1 is present in array
}

如果日期是NSDate那么

NSArray *dates = [dic objectForKey:@"date"];
for (id obj in dates)
{
    NSComparisonResult result = [currentDate compare:obj];
    if (result == NSOrderedSame)
    {
        // NSDate is present; 
        break;
    }
}
于 2013-07-30T04:20:46.943 回答