user2426008
user2426008

Reputation:

Cocoa touch how to set label to NSDate

I have a label called datelabel and I want to set it to a NSDate called dateofbirth in my AppDelgate.

I have defined and imported my AppDelgate and set it to appDelgate,but when I try self.datelabel.text = appDelgate.dateofbirth

But when I put that in it gives my this error Incompatible pointer types assigning to 'NSString *' from 'NSDate *'

I tried a few things but none of them worked.

Is there any way to set a label to a NSDate.

Upvotes: 2

Views: 328

Answers (1)

gimenete
gimenete

Reputation: 2669

You need to convert the NSDate to a NSString. You can use an NSDateFormatter for that.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// you can use one of the builtin localizated formats
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
self.datelabel.text = [formatter stringFromDate:appDelgate.dateofbirth];
// or you can set your own format
[formatter setDateFormat:@"yyyy-MM-dd"];
self.datelabel.text = [formatter stringFromDate:appDelgate.dateofbirth];

Upvotes: 1

Related Questions