通过 NSDate 值比较 NSMutableArray 的 NSMutableDictionaire
我在代码中使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用“date”作为一个键,使用“dateString”作为另一个键:
根据您显示的 plist 示例,键是“date”。假设您正在比较苹果与苹果,这就是为什么 date2 总是出现空值。
You're using "date" for one key and "dateString" for the other:
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.