如何在 xcode 中按天循环和数组索引

发布于 2024-09-27 04:22:32 字数 244 浏览 2 评论 0原文

我知道这个问题听起来有点好笑。

我正在尝试在 iPhone 项目中向下移动数组列表。

我有一个包含 100 个项目的数组: [0][1]...[99]

我想今天选择索引[0],明天选择索引[1]...100 天后索引[99]。然后在第 101 天,回到索引 [0]。

也许我需要转换 NSDate 并以日期格式获取今天的日期?那么今天是 285/365,然后对我的数组执行一些操作以根据今天的日期/日期循环它?

I know the question sounds a little funny.

I am trying to move down the list of array in my iPhone project.

I have an array of 100 items:
[0][1]...[99]

I would like to select index[0] today, index[1] tomorrow... index[99] 100 days from now. And then on day 101, back to index [0].

Maybe I need to convert NSDate and get todays date in a day format? So today would be 285/365 and then do something with my array to loop it according to today's date/day?

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

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

发布评论

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

评论(2

感受沵的脚步 2024-10-04 04:22:32

从朋友那里得到了答案。答案就是模运算!

a = Today's Date in Day format 286/365
n = Number or items in my array array.count (100)
a % n

day 1 % 100 = array[1];
day 2 % 100 = array[2];
day 99 % 100 = array[99];
day 100 % 100 = array[0];
day 101 % 100 = array[1];
day 286 % 100 = array[86];

现在我只需要研究如何将 NSDate 转换为一年中的天数。
10 月 13 日 = 286

Got the answer from a friend. Modulo Operation is the answer!

a = Today's Date in Day format 286/365
n = Number or items in my array array.count (100)
a % n

day 1 % 100 = array[1];
day 2 % 100 = array[2];
day 99 % 100 = array[99];
day 100 % 100 = array[0];
day 101 % 100 = array[1];
day 286 % 100 = array[86];

Now I just need to research on how to convert NSDate into days of the year.
October 13th = 286

画离情绘悲伤 2024-10-04 04:22:32

这个怎么样:

int index = ((int)[NSDate timeIntervalSinceReferenceDate] / 86400)) % 100;

How about this:

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