通过 NSDate 值比较 NSMutableArray 的 NSMutableDictionaire

发布于 2024-12-12 08:13:03 字数 1614 浏览 7 评论 0原文

我在代码中使用 NSArray *toWrite = [[itemssortedArrayUsingFunction:comparator context:nil]copy]; 对 NSArray 中的 NSDictionaire 进行排序。

这是我用于比较的 C 函数:

static NSComparisonResult comparator( NSDictionary *d1, NSDictionary *d2, void *context )
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEE, dd/MM/yyyy"];
    NSDate *date1 = [dateFormatter dateFromString:[d1 objectForKey:@"date"]];
    NSDate *date2 = [dateFormatter dateFromString: [d2 objectForKey: @"dateString"]];
    NSLog(@"date1:%@",[dateFormatter stringFromDate:date1]);
    NSLog(@"date2:%@",[dateFormatter stringFromDate:date2]);
    [dateFormatter release];
    NSLog(@"COMPARE:%d",[date1  compare:date2]);
    return [date1  compare:date2];
}

这是我在 NSArray 中运行带有 3 个 NSDictionaries 的应用程序时的输出:

date1:Thu, 17/11/2011
date2:(null)
COMPARE:0
date1:Sat, 19/11/2011
date2:(null)
COMPARE:0

顺便说一下,这是我的 .plist 文件中的一个 NSDictionary:

<dict>
    <key>date</key>
    <string>Thu, 17/11/2011</string>
    <key>duration</key>
    <string>120 min</string>
    <key>location</key>
    <string>BA708</string>
    <key>notes</key>
    <string>10 min reading time</string>
    <key>seat</key>
    <string>14</string>
    <key>time</key>
    <string>09:00</string>
    <key>unitName</key>
    <string>ROBOT SYSTEMS DESIGN</string>
</dict>

I'm using NSArray *toWrite = [[items sortedArrayUsingFunction:comparator context:nil] copy]; within my code to sort the NSDictionaires in my NSArray.

Here is my C function for comparing:

static NSComparisonResult comparator( NSDictionary *d1, NSDictionary *d2, void *context )
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEE, dd/MM/yyyy"];
    NSDate *date1 = [dateFormatter dateFromString:[d1 objectForKey:@"date"]];
    NSDate *date2 = [dateFormatter dateFromString: [d2 objectForKey: @"dateString"]];
    NSLog(@"date1:%@",[dateFormatter stringFromDate:date1]);
    NSLog(@"date2:%@",[dateFormatter stringFromDate:date2]);
    [dateFormatter release];
    NSLog(@"COMPARE:%d",[date1  compare:date2]);
    return [date1  compare:date2];
}

And Here is the output when I run the App with 3 NSDictionaries in the NSArray:

date1:Thu, 17/11/2011
date2:(null)
COMPARE:0
date1:Sat, 19/11/2011
date2:(null)
COMPARE:0

By the way here is how one NSDictionary from my .plist file:

<dict>
    <key>date</key>
    <string>Thu, 17/11/2011</string>
    <key>duration</key>
    <string>120 min</string>
    <key>location</key>
    <string>BA708</string>
    <key>notes</key>
    <string>10 min reading time</string>
    <key>seat</key>
    <string>14</string>
    <key>time</key>
    <string>09:00</string>
    <key>unitName</key>
    <string>ROBOT SYSTEMS DESIGN</string>
</dict>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呢古 2024-12-19 08:13:03

您使用“date”作为一个键,使用“dateString”作为另一个键:

NSDate *date1 = [dateFormatter dateFromString:[d1 objectForKey:@"date"]];
NSDate *date2 = [dateFormatter dateFromString: [d2 objectForKey: @"dateString"]];

根据您显示的 plist 示例,键是“date”。假设您正在比较苹果与苹果,这就是为什么 date2 总是出现空值。

You're using "date" for one key and "dateString" for the other:

NSDate *date1 = [dateFormatter dateFromString:[d1 objectForKey:@"date"]];
NSDate *date2 = [dateFormatter dateFromString: [d2 objectForKey: @"dateString"]];

According to the plist example you show, the key is "date." Assuming you're comparing apples to apples, that's why date2 always comes up null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文