Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 从现在开始获取所有不需要的行为_Objective C_Nsdate_Nsdatecomponents - Fatal编程技术网

Objective c 从现在开始获取所有不需要的行为

Objective c 从现在开始获取所有不需要的行为,objective-c,nsdate,nsdatecomponents,Objective C,Nsdate,Nsdatecomponents,我想在30天后拿到所有的NSDate。但是,它不能正常工作 以下是我正在使用的代码: for (int i = 0; i < days; i++) { NSDate *today = [self getToday]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents

我想在30天后拿到所有的NSDate。但是,它不能正常工作

以下是我正在使用的代码:

 for (int i = 0; i < days; i++) {

        NSDate *today = [self getToday];

        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

        NSDateComponents *components = [[NSDateComponents alloc] init];
        components.day = i;
        NSDate *nextMonth = [gregorian dateByAddingComponents:components toDate:today options:0];

        NSDateComponents *nextMonthComponents = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit fromDate:nextMonth];

        NSDateComponents *todayDayComponents = [gregorian components:NSDayCalendarUnit fromDate:today];

        nextMonthComponents.day = todayDayComponents.day+i;
        NSDate *nextMonthDay = [gregorian dateFromComponents:nextMonthComponents];
        DLog(@"nextMonthDay = %@",nextMonthDay);

        [months addObject:nextMonthDay];

    }
列表末尾是:

nextMonthDay = 2013-01-29 22:00:00 +0000
nextMonthDay = 2013-01-30 22:00:00 +0000
nextMonthDay = 2013-03-03 22:00:00 +0000
nextMonthDay = 2013-03-04 22:00:00 +0000
所以这是不正确的。相反,应该有

2013-01-29 22:00:00 +0000
2013-01-30 22:00:00 +0000
2013-03-01 22:00:00 +0000
2013-03-02 22:00:00 +0000
也许有人能帮我


谢谢。

我有不同的解决方案给你

int min = 60*60*24;
for (int i = 0; i < days; i++) {

        NSDate *today = [self getToday];
        NSDate *newDate = [today dateByAddingTimeInterval:(min*i)];
}
intmin=60*60*24;
对于(int i=0;i

希望它能帮助您

您想看看日期和时间编程指南,尤其是:[向日期添加组件]

](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html#//apple_ref/doc/uid/TP40007836-SW1)

您需要在循环中使用setDay:方法为从参考NSDate开始的每一天偏移量生成NSDate实例

        NSLog(@"30 consecutive days!");// just hello.
                                   // Set up data outside the loop...
    NSDate *today = [[NSDate alloc] init];
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
        // Surely you want to do something clever with these beyond logging them immediately, right? Let's put them in an array. They'll be in order and we can use them again later.
    NSMutableArray *thirtyConsecutiveDays = [[NSMutableArray alloc] initWithCapacity:30];
    int days = 30; // This is one less than thirty-one.
                   // Ok, let's loop!
    for (int i = 0; i < days; i++) {
            // We're going to want a new one of these each time through the loop.
        NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
            // We need to tell it how far to offset from the reference NSDate.
        [offsetComponents setDay:i]; // incrementing by i days.
                                     // We will have our current NSDateComponents friend tell our NSCalendar friend what NSDate we want next...
        NSDate *anotherDate = [gregorian dateByAddingComponents: offsetComponents
                                                         toDate:today options:0];
            // Now add our latest NSDate iteration to our array.
        [thirtyConsecutiveDays addObject:anotherDate];
    }
        // This is just logging results.
    for (NSDate *aDate in thirtyConsecutiveDays) {
        NSLog(@"%@",aDate);
    }
NSLog(@“连续30天!”);//你好。
//在循环外部设置数据。。。
NSDate*今天=[[NSDate alloc]init];
NSCalendar*格里高利=[[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
//除了立即将它们记录下来之外,您肯定还想用它们做些聪明的事情,对吗?让我们把它们放在一个数组中。它们会整理好的,我们可以稍后再使用。
NSMutableArray*ThirtyconSecuriveDays=[[NSMutableArray alloc]initWithCapacity:30];
整数天=30;//这比三十一小一。
//好的,让我们循环!
对于(int i=0;i
那么:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = 1;
NSDate *date = [self getToday];
for (int i = 1; i < 30 ; i++) {
    date = [calendar dateByAddingComponents:components toDate:date options:0];
    [months addObject:date];
}
NSCalendar*calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents*components=[[NSDateComponents alloc]init];
组分:日=1;
NSDate*date=[self getToday];
对于(int i=1;i<30;i++){
日期=[calendar dateByAddingComponents:components toDate:date选项:0];
[月添加对象:日期];
}

甚至在开始之前:为什么每次迭代都要获取“今天”?它不仅效率低下,而且如果你的算法在午夜运行,你可能会面临两个不同的日子。还有,为什么每次迭代都要创建一个新的日历?(日期不应该从01-30到02-01而不是03-01吗?你把事情搞砸了。)我不知道为什么,但今天的评论有帮助,我在循环之前添加了它,而不是更改了组件。day=I;到组件。天+=i;当DST转换时,它的工作有点棘手。第一行是什么意思?一天中没有几秒钟的魔法,只初始化一次东西。将一天添加到数组中的上一个元素并添加它。。。
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = 1;
NSDate *date = [self getToday];
for (int i = 1; i < 30 ; i++) {
    date = [calendar dateByAddingComponents:components toDate:date options:0];
    [months addObject:date];
}