【问题标题】:Can't reproduce crash that occurred during App Store review [locale issue]无法重现 App Store 审核期间发生的崩溃 [语言环境问题]
【发布时间】:2012-12-04 11:17:50
【问题描述】:

在我的更新被拒绝后,我刚刚收到了来自 Apple 的崩溃日志。

查看崩溃日志后,我发现了与解析日期有关的违规代码。

当我在我的设备上运行此应用程序时,我没有收到此错误。我感觉这是 Apple 正在测试的设备的本地问题。

在我运行期间,下面的original 字符串解析为2012-12-04T11:02:29.5600000+0000。我只是想知道如果在不同的语言环境中它会以什么其他方式出现。

更重要的是,如何在与 Apple 相同的环境中测试我的应用,这样我就无需等待审核流程进行测试

注意:我继承了此代码,因此非常感谢任何有关改进日期解析功能的建议,谢谢。

崩溃来自以下函数中的调用[original substringToIndex:range.location+3]

+(NSString*)convertDate:(NSDate*)date withFormatter:(NSDateFormatter*)dateFormatter{

    NSString *original = [dateFormatter stringFromDate:date];

    NSRange range = [original rangeOfString:@"+"];
    NSString *substring = [original substringToIndex:range.location+3];
    NSString *substring2 = [original substringFromIndex:range.location+3];

    return [NSString stringWithFormat:@"%@%@%@",substring,@":",substring2];
}

在以下上下文中调用此函数

NSDate *timestamp = [NSDate date];
NSString *dateString = [DateHelper convertDate:timestamp withFormatter:UTCDateFormatter];

【问题讨论】:

    标签: objective-c ios local


    【解决方案1】:

    说 +3 完全有可能导致出现超出范围的异常。您是否测试过使用 [NSDate date] 会在其他时区返回相同的格式?

    无论哪种方式,您都应该至少检查两件事 - 然后解析多个日期类型的方法以确认应用程序仍然可以运行:

    1. 下一行的范围有一个位置:

      NSRange range = [original rangeOfString:@"+"];
      if (range.location != NSNotFound)
      {
          //do stuff here
      }
      
    2. range.location + 你输入的 3 不超过字符串总长度

      NSRange range = [original rangeOfString:@"+"];
      if (range.location != NSNotFound)
      {
         if ((range.location + 3) <= original.length)
         {
             //you should also check that ((range.location + 3) + range.length) <= original.length as well
             //do stuff here
         }
      }
      

    虽然上述内容并不能真正解决可能显示不正确日期/时间戳的问题 - 它应该防止应用程序尝试寻找超出字符串长度范围的范围或索引

    希望这会有所帮助,

    【讨论】:

    • 原来 *original 字符串包含一个 ISO8601 日期时间字符串。所以我只是退回了那个,一切都很好。顺便说一句,这是一个越界异常。
    【解决方案2】:

    NSDateFormatter 将使用您的设备日期格式和时区格式化您的日期。

    // Convert the date to current time zone    
        NSDate* sourceDate = [NSDate date];
    
        NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
        NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
    
        NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
        NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
        NSTimeInterval interval = sourceGMTOffset - destinationGMTOffset;
    
        NSDate *destinationDateHere1 = [[NSDate alloc] initWithTimeInterval:interval sinceDate:yourDate];
    

    【讨论】:

      猜你喜欢
      • 2016-10-05
      • 2011-04-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      • 2018-05-22
      相关资源
      最近更新 更多