首页 > 解决方案 > 我为聊天应用程序编写了代码,适用于 iOS 12,但在 iOS 13 中,它返回 nil 值

问题描述

在此处输入图像描述我为聊天应用程序编写了代码,在 iOS 12 上运行良好,在 iOS 13 中,它返回 nil 值。

这是我的 dateformatter 代码片段。

-(NSDate *)stringToDateNew:(NSString*)strDate withForamt:(NSString *)strFormat {
   @try
   {
       NSDate *myDate;

       if(strDate!=NULL)
       {
           NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
           [dateFormat setDateFormat:[NSString stringWithFormat:@"%@",strFormat]];
           NSLocale * enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] ;
           if (enUSPOSIXLocale == nil) {
               enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
           }
           else
           {
            [dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
            [dateFormat setLocale:enUSPOSIXLocale];
            myDate = [dateFormat dateFromString:strDate];
            return myDate;
           }

       }
       return NULL;

   }
   @catch (NSException *exception)
   {
       NSLog(@"Excption in AppDelegate : stringToDate %@:%@",exception.name,exception.reason);
   }
}

标签: iosobjective-cnsdateformatter

解决方案


使用 withformat "yyyy-mm-dd'T'hh:mm:ss.SSS"。它也适用于 os 13。


推荐阅读