从 NSString 解析 NSDate 时出错

发布于 2025-01-02 10:45:41 字数 1503 浏览 3 评论 0原文

当我点击一行时,我的应用程序冻结,这是 didSelectRowAtIndexPath 的代码,stringDate 等于这样的字符串:Feb 4

EditView *editController = [[EditView alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:editController animated:YES];

NSDictionary *cellValue = [self.array objectAtIndex:[indexPath row]];
editController.savedString = [cellValue objectForKey:@"label"];

NSDateFormatter *df = [[NSDateFormatter alloc] init];
NSString *stringDate = [cellValue valueForKey:@"date"];

NSDate *parsedDate = [df dateFromString:stringDate];
editController.savedDate = parsedDate;

[editController release];

[self.tableView deselectRowAtIndexPath:indexPath animated:YES];

NSString *stringDate = [cellValue valueForKey:@"date"]; 行点击行时出现此错误,并出现以下错误:

* 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效参数不满足:日期”

更新: 解析的日期设置为 UIDatePicker 带有日期和;时间模式,我只是将字符串日期转换为 NSDate 以便与 [timePicker setDate:savedDateanimated:YES];

我创建了 cellValue code> 作为一个 NSDictionary ,它基本上访问一个 plist ,我在其中创建了一个 NSDictionary ,然后用格式化的 NSString 编写它>。 array 显示 plist 数组,因此我可以使用 [cellValue objectForKey:@"date"] 访问它

NSDictionary *cellValue = [self.array objectAtIndex:[indexPath row]];

My app freezes when I tap a row, here is the code for didSelectRowAtIndexPath, the stringDate is equal to a string like this: Feb 4.

EditView *editController = [[EditView alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:editController animated:YES];

NSDictionary *cellValue = [self.array objectAtIndex:[indexPath row]];
editController.savedString = [cellValue objectForKey:@"label"];

NSDateFormatter *df = [[NSDateFormatter alloc] init];
NSString *stringDate = [cellValue valueForKey:@"date"];

NSDate *parsedDate = [df dateFromString:stringDate];
editController.savedDate = parsedDate;

[editController release];

[self.tableView deselectRowAtIndexPath:indexPath animated:YES];

I get this error when tapping the row, on the line NSString *stringDate = [cellValue valueForKey:@"date"]; with this error:

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date'

UPDATE: The parsed date is set to a UIDatePicker with date & time mode, and I am simply converting the string date to a NSDate to be used with [timePicker setDate:savedDate animated:YES];

I created cellValue as a NSDictionary which basically accesses a plist where I created a NSDictionary and then wrote it with a formatted NSString. And array displays the plist array, so I can access it using [cellValue objectForKey:@"date"]

NSDictionary *cellValue = [self.array objectAtIndex:[indexPath row]];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡写薰衣草的香 2025-01-09 10:45:41

您需要告诉格式化程序您的字符串满足的格式

它应该是这样的:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MMM d"];

You need to teach the formatter the format your string fulfills

It should be something like:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MMM d"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文