添加缺失的对象以创建有序集合

发布于 2024-09-28 09:23:22 字数 536 浏览 8 评论 0原文

这个主题很模糊,因为我不知道如何用一句话表达我想要的东西。

这里是:

我有一个 NSDictionaries 的 NSArray。每个 NSDictionary 代表日历年中的一天。每个 NSDictionary 都有一个键“date”,其值为 NSDate。数组中应该有 365 个 NSDictionary 项。该词典是由我无法控制的服务器创建的,有时会丢失多达 100 天。

我需要确保数组有 365 个字典,每个字典都晚一天。

我目前按日期对数组进行排序,迭代它,将 NSDictionaries 从当前数组复制到新数组。这样做时,我将当前词典的日期值与下一个词典的日期值进行比较。如果两个日期之间的间隔超过一天,我会向新数组添加足够的新字典来覆盖那些缺失的日期(并相应地设置它们的日期),然后继续。

由于日期应该是有序的,我想知道框架或语言中是否还没有一种机制可以用来说“这是一个数组,这个键路径应该是连续的。查找并创建以下元素:缺少,这里有一个块或方法可以用来初始化它们”。

我的方法有些地方实施得不好,所以我向你求助。想法?

谢谢。

The subject is vague because I'm not sure how to articulate in one sentence what I want.

Here goes:

I have an NSArray of NSDictionaries. Each NSDictionary represents one day of the calendar year. Each NSDictionary has a key "date" with a value of NSDate. There should be 365 NSDictionary items in the array. The dictionary is created by a server that I don't control, and it sometimes is missing as many as 100 days.

I need to ensure the array has 365 dictionaries, each one day later than the next.

I currently sort the array by date, iterate through it, copying the NSDictionaries from the current array to a new array. While so doing, I compare the current Dictionary's date value with the date value for the next dictionary. If there is more than one day between the two dates, I add enough new dictionaries to the new array to cover those missing days (and set their dates accordingly), then continue through.

Since the dates are supposed to ordered, I wonder if there is not already a mechanism in the framework or language that I can use to say "Here is an array, and this keypath is supposed to be consecutive. Find and create the elements that are missing, and here's a block or method you can use to initialize them".

Something about my method just feels poorly implemented, so I turn to you. Thoughts?

Thanks.

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

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

发布评论

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

评论(3

故乡的云 2024-10-05 09:23:22

你这样做的方式听起来非常理智,据我所知,没有什么可以在基本框架中自动完成它。

The way you did it sounds perfectly sane, and there is nothing to my knowledge that will do it automatically in the base framework.

小鸟爱天空丶 2024-10-05 09:23:22

此代码将对它们进行排序。

NSArray *dates;  // wherever you get this...
NSArray *sortedDates = [dates sortedArrayUsingComparator:^(id obj1, id obj2) 
    {
    return [[obj1 valueForKey:@"date"] compare:[obj2 valueForKey:@"date"]];
    }];

至于创建缺失的条目,您必须自己完成。

This code will sort them.

NSArray *dates;  // wherever you get this...
NSArray *sortedDates = [dates sortedArrayUsingComparator:^(id obj1, id obj2) 
    {
    return [[obj1 valueForKey:@"date"] compare:[obj2 valueForKey:@"date"]];
    }];

As for creating the missing entries, you'll have to do that yourself.

小嗲 2024-10-05 09:23:22

您不需要进行排序:

  1. 创建一个包含 365(或 366)个占位符字典的数组(您可以对所有插槽使用相同的字典,或使用 NSNull)
  2. 迭代传入的数组并找出每天是哪一天字典是为了。将每个字典放入数组中正确的位置。

You don't need to do the sort:

  1. Create an array with 365 (or 366) placeholder dictionaries (you can possibly use the same one for all slots, or use NSNull)
  2. iterate through the passed in array and figure out which day each of the dictionaries is for. Place each dictionary in its rightful slot in your array.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文