NSDateFormatter.dateFromString for循环,用@try处理无效格式

NSDateFormatter.dateFromString in for loop, with @try to handle invalid format

我正在尝试从字典中解析日期字符串,可能格式无效。所以我循环字典并尝试在 @try @catch.

中用 DateFormatter.dateFromString 解析它们
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MMM-yyyy"];

for(NSDictionary *mainSettlementData in mainSettlementDataList){

    NSString *dateString = [mainSettlementData objectForKey:@"settlementDate1"];
    if(dateString){
        NSLog(@"1");
        NSLog(@"dateString = %@", dateString);
        @try{
            NSDate *settlementDate = [dateFormatter dateFromString:dateString];
            NSLog(@"converted NSDate = %@", settlementDate);
        }
        @catch(NSException *e){
            NSLog(@"Error parsing settlementDate : %@", e.description);
        }
        NSLog(@"2");
    }
    else{
        NSLog(@"3");
    }
}
NSLog(@"Finish Loading Date");

在输入格式无效之前,NSLog 的结果是好的

2015-08-01 17:42:37.080 TrinityBackOffice[878:132472] 1
2015-08-01 17:42:37.081 TrinityBackOffice[878:132472] dateString = 05-Aug-2014
2015-08-01 17:42:37.084 TrinityBackOffice[878:132472] converted NSDate = 2014-08-04 17:00:00 +0000
2015-08-01 17:42:37.084 TrinityBackOffice[878:132472] 2
2015-08-01 17:42:37.084 TrinityBackOffice[878:132472] 1
2015-08-01 17:42:37.084 TrinityBackOffice[878:132472] dateString = 02-Oct-2012
2015-08-01 17:42:37.084 TrinityBackOffice[878:132472] converted NSDate = 2012-10-01 17:00:00 +0000
2015-08-01 17:42:37.085 TrinityBackOffice[878:132472] 2
2015-08-01 17:42:37.085 TrinityBackOffice[878:132472] 1
2015-08-01 17:42:37.085 TrinityBackOffice[878:132472] dateString = 05-Aug-2014
2015-08-01 17:42:37.085 TrinityBackOffice[878:132472] converted NSDate = 2014-08-04 17:00:00 +0000
2015-08-01 17:42:37.085 TrinityBackOffice[878:132472] 2
2015-08-01 17:42:37.085 TrinityBackOffice[878:132472] 1
2015-08-01 17:42:37.086 TrinityBackOffice[878:132472] dateString = 05-Aug-2014
2015-08-01 17:42:37.086 TrinityBackOffice[878:132472] converted NSDate = 2014-08-04 17:00:00 +0000
2015-08-01 17:42:37.086 TrinityBackOffice[878:132472] 2
2015-08-01 17:42:37.087 TrinityBackOffice[878:132472] 1
2015-08-01 17:42:37.088 TrinityBackOffice[878:132472] dateString = <null>
2015-08-01 17:42:37.088 TrinityBackOffice[878:132472] -[NSNull length]: unrecognized selector sent to instance 0xbabb48
2015-08-01 17:42:37.088 TrinityBackOffice[878:132472] Error parsing settlementDate : -[NSNull length]: unrecognized selector sent to instance 0xbabb48
2015-08-01 17:42:37.089 TrinityBackOffice[878:132472] 2
2015-08-01 17:42:37.089 TrinityBackOffice[878:132472] 1
2015-08-01 17:42:37.089 TrinityBackOffice[878:132472] dateString = 02-Oct-2012

然后记录停止,应用冻结。最后一条日志 NSLog(@"Finish Loading Date");永远不要打印出来。请注意,我在应用程序的第一个 ViewController 的 viewDidLoad 中执行此操作,因此应用程序会冻结在初始屏幕页面。

另请注意,如果字典中没有无效的日期字符串,则该应用程序可以正常运行。如果我删除行

NSDate *settlementDate = [dateFormatter dateFromString:dateString];

该应用程序也运行良好。

请帮忙,谢谢!

PS。想要的数据源就在这里

NSData *data =
[@"{\"asOfDate\":\"23-Jul-2015\",\"portfolioData\":[{\"costValue\":11464.614,\"accountID\":\"99-91418-4\",\"mktValue\":10800,\"unrealizedAmt\":-664.61},{\"costValue\":0,\"accountID\":\"999-091418-0-3\",\"mktValue\":0,\"unrealizedAmt\":46600}],\"settlementData\":[{\"settlementDate2\":null,\"accountID\":\"99-00427-1\",\"netAmount3\":0,\"bankSym\":\"BBL\",\"settlementDate1\":\"05-Aug-2014\",\"netAmount1\":0,\"netAmount2\":0,\"bankAcc\":\"1310689722\",\"settlementDate3\":null},{\"settlementDate2\":\"05-Aug-2014\",\"accountID\":\"99-00427-4\",\"netAmount3\":3075.06,\"bankSym\":\"BBL\",\"settlementDate1\":\"02-Oct-2012\",\"netAmount1\":-86858.6,\"netAmount2\":-13096.74,\"bankAcc\":\"1310689722\",\"settlementDate3\":\"07-Aug-2014\"},{\"settlementDate2\":\"06-Aug-2014\",\"accountID\":\"99-00427-0\",\"netAmount3\":-226381.6,\"bankSym\":\"BBL\",\"settlementDate1\":\"05-Aug-2014\",\"netAmount1\":-442746.3,\"netAmount2\":323452.94,\"bankAcc\":\"1310689722\",\"settlementDate3\":\"07-Aug-2014\"},{\"settlementDate2\":\"06-Aug-2014\",\"accountID\":\"99-00427-0\",\"netAmount3\":-226381.6,\"bankSym\":\"BBL\",\"settlementDate1\":\"05-Aug-2014\",\"netAmount1\":-442746.3,\"netAmount2\":323452.94,\"bankAcc\":\"1310689722\",\"settlementDate3\":\"07-Aug-2014\"},{\"settlementDate2\":null,\"accountID\":\"99-00427-1\",\"netAmount3\":0,\"bankSym\":\"BBL\",\"settlementDate1\":null,\"netAmount1\":0,\"netAmount2\":0,\"bankAcc\":\"1310689722\",\"settlementDate3\":null},{\"settlementDate2\":\"05-Aug-2014\",\"accountID\":\"99-00427-4\",\"netAmount3\":3075.06,\"bankSym\":\"BBL\",\"settlementDate1\":\"02-Oct-2012\",\"netAmount1\":-86858.6,\"netAmount2\":-13096.74,\"bankAcc\":\"1310689722\",\"settlementDate3\":\"07-Aug-2014\"}],\"transactionStatus\":[]}" dataUsingEncoding:NSUTF8StringEncoding];

NSError *myError = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&myError];
NSArray *mainSettlementDataList = [responseDictionary objectForKey:@"settlementData"];

您的数据在 settlementDate1 return 中为空值,因此您检查条件日期不为空然后设置日期格式。

你试试这个代码:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd-MMM-yyyy"];

        for(NSDictionary *mainSettlementData in mainSettlementDataList){

            NSString *dateString = [mainSettlementData objectForKey:@"settlementDate1"];
            if(![dateString isEqual:[NSNull null]]){
                NSLog(@"1");
                NSLog(@"dateString = %@", dateString);

                NSDate *settlementDate = [dateFormatter dateFromString:dateString];
                NSLog(@"converted NSDate = %@", settlementDate);
            }
            else{
                NSLog(@"3");
            }
        }
        NSLog(@"Finish Loading Date");

或使用此条件检查空值

if(dateString!=nil && ![dateString isEqualToString:@"null"] && dateString!=(id)[NSNull null]){
     // value
}else{
    // null value
}