brush51
brush51

Reputation: 5771

how to format NSDate from coreData and display it correctly

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.

Upvotes: 0

Views: 2996

Answers (2)

Nikita Ivaniushchenko
Nikita Ivaniushchenko

Reputation: 1435

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

Upvotes: 2

Gabriel
Gabriel

Reputation: 3359

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"] ;

Upvotes: 4

Related Questions