NSDate在设备上返回null而不是在模拟器上

时间:2013-08-13 03:43:44

标签: ios null ios-simulator nsdate

之前我发布了这个问题并得到了一个有效的答案,只有一个晚上:(

我有这段代码:

NSLog(@"timeStringsArray2 %@", timeStringsArray2);

    //3. Create NSDateFormatter
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm a"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"MST"]];

    //4. Get strings from Array
    NSString *openDateString = (NSString*)[timeStringsArray2 objectAtIndex:0];
    NSString *closeDateString = (NSString*)[timeStringsArray2 objectAtIndex:1];
    NSLog(@"someDateString %@,%@", openDateString,closeDateString);

    //5. Get month & day of current date
    NSDate *currentDate = [NSDate date];
    NSCalendar* calendario = [NSCalendar currentCalendar];
    NSDateComponents* components = [calendario components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentDate];

    [components year]; // gives you year

    //6. Make new strings with year 0001 + original time
    NSString *adjustedOpenDateString = [NSString stringWithFormat:@"%@-%ld-%ld %@", @"2013", (long)[components month], (long)[components day], openDateString];
    NSString *adjustedCloseDateString = [NSString stringWithFormat:@"%@-%ld-%ld %@", @"2013", (long)[components month], (long)[components day],closeDateString];
    NSLog(@"adjustedString %@", adjustedCloseDateString);

    //7. Use Date Formatter to convert adjusted strings to NSDates
    NSDate *openDate = [dateFormatter dateFromString:adjustedOpenDateString];
    NSDate *closeDate = [dateFormatter dateFromString:adjustedCloseDateString];

    NSLog(@"DEALWITHTIMESTRINGS = open, now, close %@,%@,%@", openDate,[NSDate date], closeDate);

记录下来:

timeStringsArray2 (
    "8:00AM",
    "6:30PM" ) 2013-08-12 
09:28:49 p.m.:316[7008:2311] +[TimeComparator dealWithTimeStrings2:] [Line 59] someDateString 8:00AM,6:30PM 2013-08-12 
09:28:49 p.m.:320[7008:2311] +[TimeComparator dealWithTimeStrings2:] [Line 73] adjustedString 2013-8-12 6:30PM 2013-08-12 
09:28:49 p.m.:325[7008:2311] +[TimeComparator dealWithTimeStrings2:] [Line 79] DEALWITHTIMESTRINGS = open, now, close (null),2013-08-13 04:28:49 a.m. +0000,(null)

我被建议将格式更改为hh:mma。我做了,它昨晚做了很短暂的工作,也许是几次。今天早上它停止工作,再次返回null。还有其他想法吗?

我发现的唯一模式是它可以在模拟器上运行,但不能在任何设备上运行。在模拟器中,所有日期都会恢复正常,开启和关闭。但是当我在设备上运行它时,所有日期都会返回null。

0 个答案:

没有答案