我有一些代码用于对日历日期进行排序,如下所示:

#if !(TARGET_IPHONE_SIMULATOR)
    NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"HH:mm dd MMM yyyy" options:0
                                                              locale:[NSLocale currentLocale]];
    [fmt setDateFormat:formatString];
#else
    [fmt setDateFormat:@"HH:mm dd MMM yyyy"];
#endif

如果我在模拟器中运行它,一切正常.如果我在设备上运行它,我会收到这个讽刺的调试消息.

2012-09-19 22:40:13.972 APPNAME [4923:907] *-[__NSCFCalendar 组件:FromDate:]:日期不能为零

我是说真的,你怎么 你认为那个手术应该意味着没有约会吗?

目前已经避免了一个例外.

其中几个错误将会是 与这一投诉一起报告,那么进一步的违规行为将只需 默默地做任何由零产生的随机的事情.这是 回溯这次发生此情况的位置(某些帧可能丢失 由于编译器优化):

你不得不嘲笑它,但我不确定我的dateFormatFromTemplate:代码出了什么问题.任何帮助都将不胜感激.

Running Xcode V 4.5 btw


更新:

回溯:

0 CoreFoundation 0x39ff0e55+84
1个 应用名称0x00040be1 -[MeetingsViewController dateAtBeginningOfDayForDate:]+140

所以我猜在我的dateAtBeginningOfDayForDate方法中情况会变得很糟糕.看起来是这样的:

/*
 Break down a given NSDate to its bare components for sorting
 */
- (NSDate *)dateAtBeginningOfDayForDate:(NSDate *)inputDate
{
    // Use the user's current calendar and time zone
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    [calendar setTimeZone:timeZone];

    // Selectively convert the date components (year, month, day) of the input date
    NSDateComponents *dateComps = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:inputDate];

    // Set the time components manually
    [dateComps setHour:0];
    [dateComps setMinute:0];
    [dateComps setSecond:0];

    // Convert back
    NSDate *beginningOfDay = [calendar dateFromComponents:dateComps];
    return beginningOfDay;
}

我使用此方法将给定的NSDate分解为其核心组件,以便更有效地对它们进行排序.然而,我的应用程序必须是国际的,这就是使用NSLocale来格式化数据输出的原因.从目前看来,我需要改变我的dateAtBeginningOfDayForDate,以便也能在国际上工作.

推荐答案

非常感谢nneonneo帮我解决了这个问题.

我的问题是,我的应用程序必须在国际上运行,所以我需要显示与用户位置相关的日期.我认为我应该为用户区域设置创建一个NSDateFormatter,然后传入一个字符串日期,如下所示:

 NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"HH:mm dd MMM yyyy" options:0
                                                              locale:[NSLocale currentLocale]];
date = [fmt dateFromString:[NSString stringWithFormat:@"%@ %@", obj.meetingTime, obj.meetingDate]];

然而,问题是我本地化了我在字符串date中传递的日期格式before.在我的例子中,本地化后的NSDateFormatter是这样的.

MMM dd, yyyy, HH:mm

本地化后的格式在本地化后可能看起来有许多不同的方式.

因此,传入格式为HH:mm dd MMM yyyy的字符串会导致返回nil.

Which is obvious now-.-

因此,我没有在创建日期时进行本地化,而是创建了一个标准格式为HH:mm dd MMM yyyy的日期.然后,在显示时,我将NSDate格式化为用户各自的区域设置.

Ios相关问答推荐

Flutter应用程序无法使用IOS/Swift的蓝牙核心库发现某些外围设备

如何在SwiftUI中以一定Angular 绘制直线(&A)

为什么IOS多行文本输入在React原生纸张对话框中无法正常工作?

比较 `某个协议` 实例时出现编译错误

NumberFormatter 无法使用 Xcode 15.0 beta 正确识别 iOS 17 beta 中的 Locale 货币设置

创建 Flutter 项目时,如何从我的系统中删除特定的开发者身份?

uikit中如何触发swiftui功能

SwiftUI 每秒从 Timer 更新单个 @State 属性,每秒也重绘整个视图

Flutter firebase_auth 中的无效应用程序凭据/令牌不匹配

发生异常:需要 issuerId

Xcode14 启动时崩溃:dyld:库未加载:/usr/lib/swift/libswiftCoreGraphics.dylib

expo EAS build (iOS) 在 Pod 安装步骤中失败 (SDK45 & 46)

如何包含 Xcode 子项目标头?

无法初始化 FirebaseApp - Flutter

Xcode 6 App Store 提交失败并显示您的帐户已经拥有有效的 iOS 分发证书

是否可以使用 sharedHTTPCookieStorage 为 UIWebView 手动设置 cookie?

标题为 UIImage 的导航栏

无法加载从带有标识符的包中的笔尖引用的图像

/usr/bin/codedesign 失败,退出代码为 1

Xcode 5 和 iOS 7:架构和有效架构