iOS NSDateFormatter dateFromString 仅在一个实例中返回 nil [英] iOS NSDateFormatter dateFromString returns nil only in one instance

查看:53
本文介绍了iOS NSDateFormatter dateFromString 仅在一个实例中返回 nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSDateFormatter 是否有意外返回 nil 的倾向?我不认为它做到了.我希望我忽略了一些我看不到的微小细节.

Does NSDateFormatter have a propensity for unexpectedly returning nil? I wouldn't think it did. I am hoping that I have overlooked some miniscule detail which for whatever reason I cannot see.

我有一些描述太阳和月亮的数据.它列在下面.我还有一个循环,一旦它从它的 plist 加载到字典中,它就会迭代这个数据.最后,我有一个类,它使用 NSDateFormatter 作为单例为我处理日期.这个过程运行良好,这些方法已经相互作用并产生了正确的日期一两个月,但今天它崩溃了.所有正在创建并通过这个简单过程传递的字符串都被正确地转换为日期,除了最后一个为零.这是发生的事情:

I have some data describing the sun and moon. It's listed below. I also have a loop which iterates over this data once it is loaded from its plist into a dictionary. Finally I have a class which processes dates for me using NSDateFormatter as a singleton. This process has worked pretty well and these methods have been interacting and producing correct dates for a month or two, but today it crashed. All of the strings being created and passed through this simple process are correctly converted into dates except the last one which is nil. Here's what happened:

这是属性列表中的数据.

This is the data in the property list.

<key>2014-3-7</key>
<dict>
    <key>moonrise</key>
    <string>10:51 am</string>
    <key>moonset</key>
    <string>0:03 am</string>
    <key>phase</key>
    <string>Waxing Crescent</string>
    <key>sunrise</key>
    <string>6:39 am</string>
    <key>sunset</key>
    <string>6:21 pm</string>
</dict>

<key>2014-3-8</key>
<dict>
    <key>moonrise</key>
    <string>11:39 am</string>
    <key>moonset</key>
    <string>0:56 am</string>
    <key>phase</key>
    <string>First Quarter</string>
    <key>sunrise</key>
    <string>6:38 am</string>
    <key>sunset</key>
    <string>6:21 pm</string>
</dict>

<key>2014-3-9</key>
<dict>
    <key>moonrise</key>
    <string>1:28 pm</string>
    <key>moonset</key>
    <string>2:45 am</string>
    <key>phase</key>
    <string></string>
    <key>sunrise</key>
    <string>7:37 am</string>
    <key>sunset</key>
    <string>7:22 pm</string>
</dict>

此数据通过循环处理,从从昨天开始到明天结束的日期字符串创建日期,因此在这种情况下,它从 7 日或 3 月开始,到 9 日结束,我们正在处理每个 plist 项目中的值给定日期作为名为 iDay 的字典.

This data is processed through a loop creating dates from date strings starting with yesterday and ending tomorrow, so in this case it starts on the 7th or march and ends on the 9th and we are working with the values in each plist item for the given date as a dictionary called iDay.

NSString *eventString;
NSDate *moonrise;
NSDate *moonset;
NSString *dateFormatString = @"yyyy-MM-dd h:mm a";

if ([iDay objectForKey:@"moonrise"] != nil) {
    eventString = [NSString stringWithFormat:@"%@ %@", dateString, [iDay objectForKey:@"moonrise"]];
    moonrise = [self.dateTime makeDateFromString:eventString
                                          format:dateFormatString];
}

if ([iDay objectForKey:@"moonset"] != nil) {
    eventString = [NSString stringWithFormat:@"%@ %@",dateString, [iDay objectForKey:@"moonset"]];
    moonset = [self.dateTime makeDateFromString:eventString
                                         format:dateFormatString];
}

这些语句传递给 [self.dateTime makeDateFromString] 的字符串如下所示:

The strings being passed to [self.dateTime makeDateFromString] by these statements look like this:

2014-3-7 10:51 上午

2014-3-7 10:51 am

2014-3-7 0:03 上午

2014-3-7 0:03 am

2014-3-8 11:39 上午

2014-3-8 11:39 am

2014-3-8 0:56 上午

2014-3-8 0:56 am

2014-3-9 下午 1:28

2014-3-9 1:28 pm

2014-3-9 2:45 上午

2014-3-9 2:45 am

好的,好的.现在 dateTime 单例:

Okay, good. Now the dateTime singleton:

+(ADateTimeClass *) sharedDateTime {
    @synchronized(self)
    {
        if (sharedInstance == NULL)
            sharedInstance = [[self alloc] init];
    }
    return sharedInstance;
}

-(id) init{
    self = [super init];
    if(self){
        self.dateFormat = [NSDateFormatter new];
        self.dateFormat.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    }
    return self;
}

... 以及一切都出错的方法.传递给此方法的所有字符串都作为日期正确返回,除了最后一个,我已经检查过,nil 来自此处的[dateFormat dateFromString:dateString]",其中 dateString 是我上面列出的六个之一.

... and the method where it all goes horribly wrong. All of the strings passed to this method get returned correctly as dates except the last one, and I've checked, the nil is coming from "[dateFormat dateFromString:dateString]" right here where dateString is one of the six I listed above.

-(NSDate*) makeDateFromString:(NSString *)dateString format:(NSString *)format{
    [dateFormat setDateFormat:format];
    NSDate* aDate = [dateFormat dateFromString:dateString];
    return aDate;
}

结果:

月出打印说明:2014-03-07 14:51:00 +0000

Printing description of moonrise: 2014-03-07 14:51:00 +0000

月落印刷说明:2014-03-07 04:03:00 +0000

Printing description of moonset: 2014-03-07 04:03:00 +0000

月出打印说明:2014-03-08 15:39:00 +0000

Printing description of moonrise: 2014-03-08 15:39:00 +0000

月落印刷说明:2014-03-08 04:56:00 +0000

Printing description of moonset: 2014-03-08 04:56:00 +0000

月出打印说明:2014-03-09 16:28:00 +0000

Printing description of moonrise: 2014-03-09 16:28:00 +0000

月落打印说明:<------------ !!!!!!嗯???

Printing description of moonset: <nil> <------------ !!!!!! Huh???

推荐答案

这是一个简单的问题,实际上取决于你住在哪里.至少在美国,由于夏令时的变化,从 2014 年 3 月 9 日凌晨 2 点到凌晨 2 点 59 分之间的任何时间都不存在.因此任何这样的日期都是无效的.

It's a simple problem actually depending on where you live. At least in the USA, any time from March 9 2014 between 2am and 2:59am won't exist due to daylight saving time change. Therefore any such date is invalid.

这篇关于iOS NSDateFormatter dateFromString 仅在一个实例中返回 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆