开发者

How to loop and array's index by day in xcode

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?


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


How about this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜