如何从 coreData 格式化 NSDate 并正确显示

发布于 2024-12-22 13:20:57 字数 537 浏览 4 评论 0原文

我想格式化来自 coreData 的日期。 我使用这行代码从 coredata 获取日期:

[[managedObject valueForKey:@"presentationDate"] description]

我知道我可以使用 NSDateFormatter 格式化 NSDate。

这是我的代码:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd.MMM.yyyy"];

NSDate *date = (NSDate *) 
[[managedObject valueForKey:@"presentationDate"] description];

NSLog(@"theDate: |%@| \n", [dateFormat stringFromDate:date]);//output is null

输出为空。我找不到正确的方法来正确显示日期。请帮忙。

i want to format my date which comes from coreData.
i am getting the date from coredata with this line of code:

[[managedObject valueForKey:@"presentationDate"] description]

i know that i can format the NSDate with NSDateFormatter.

here is my code:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd.MMM.yyyy"];

NSDate *date = (NSDate *) 
[[managedObject valueForKey:@"presentationDate"] description];

NSLog(@"theDate: |%@| \n", [dateFormat stringFromDate:date]);//output is null

The output ist null. I cant find the right way to put out the date correctly. please help.

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

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

发布评论

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

评论(2

怎樣才叫好 2024-12-29 13:20:57

如果presentationDate是一个DATE并且它的类是托管对象中的DATE,那么错误就在这里

NSDate *date = (NSDate *) 
[[managedObject valueForKey:@"presentationDate"] description];

你最好这样写

NSDate *date = (NSDate*) [managedObject valueForKey:@"presentationDate"] ;

If presentationDate is a DATE and its class is DATE in the managed object, then the error is here

NSDate *date = (NSDate *) 
[[managedObject valueForKey:@"presentationDate"] description];

You'd better writing this

NSDate *date = (NSDate*) [managedObject valueForKey:@"presentationDate"] ;
初心未许 2024-12-29 13:20:57

-description方法总是返回描述对象的NSString
因此,从核心数据获取对象时一定不要调用它。

-description method always returns NSString that describes object.
So, you mustn't call it when getting object from core data.

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