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

标签 ios objective-c try-catch nsdateformatter

我正在尝试从字典中解析日期字符串,可能格式无效。所以我循环字典并尝试在 @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];

该应用也运行良好。

请帮忙,谢谢!

附言。如果您想拥有数据源,请点击此处。

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 中的数据返回空值,因此您检查条件日期是否不为空,然后设置日期格式。

你试试这段代码:

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
} 

关于ios - NSDateFormatter.dateFromString for 循环,用@try处理无效格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31761043/

相关文章:

ios - iOS 中的 DBL_EPSILON 问题

ios - 有没有办法在片段着色器中使用64个以上的向量?或替代?

ios - 将参数传递给方法 - UIPanGestureRecognizer

python - 为什么这个 forloop 会跳过列表中的项目?

ios - 如何检测新的iBeacon?

ios - 我们能得到 UILabel 的名字吗?

java - 无法使用 MyBatis 在 Java 中捕获 SQLSyntaxErrorException

c++ - 带有 try catch 和异常抛出的方法

iPhone 3.1 SDK 相机访问

ios - 如何区分UITableView自定义section点击和section第一行点击?