Joe24
Joe24

Reputation: 161

Pulling NSDate from Core Data and displaying it IOS

I am making an iPhone App and I have an NSDate in Core data and I am trying to display it but I keep getting this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDate isEqualToString:]: unrecognized selector sent to instance 0x81b7840'.

The code I have below is pulling the data from item which is a dictionary. How can I get this to work?

self.lblTheDate.text = [self.item valueForKey:@"MyDate"];

Upvotes: 1

Views: 501

Answers (2)

Robotic Cat
Robotic Cat

Reputation: 5891

Use something similar to the following to convert an NSDate (in your dictionary) into an NSString but select the correct date and time styles you require:

self.lblTheDate.text = [NSDateFormatter localizedStringFromDate:[self.item valueForKey:@"MyDate"] dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle]

There are various options for the date & time styles:

NSDateFormatterNoStyle     : Specifies no style,
NSDateFormatterShortStyle  : Specifies a short style, typically numeric only, such as “11/23/37” or “3:30pm”,
NSDateFormatterMediumStyle : Specifies a medium style, typically with abbreviated text, such as “Nov 23, 1937”,
NSDateFormatterLongStyle   : Specifies a long style, typically with full text, such as “November 23, 1937” or “3:30:32pm”,
NSDateFormatterFullStyle   : Specifies a full style with complete details, such as “Tuesday, April 12, 1952 AD” or “3:30:42pm PST”

Note that the format for these date and time styles is not exact because they depend on the locale, user preference settings and the operating system version.

Class Reference: NSDateFormatter

Upvotes: 3

AlexWien
AlexWien

Reputation: 28727

it seems that you get a NSDate object and want to assign it to a text field that wants a NSString as argument.
you must first convert the date to a NSstring, for example using NSDateFormat

Upvotes: 0

Related Questions