Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How can I save my datepicker date to Parse?

Hey guys, I set my UIDatePicker to just time only and Im wondering how to save that time when I click my button to Parse.

7 Answers

Hi Mike!

Try this:

// get date from NSDatePicker
NSDate *date = [datePicker date];

// format the NSDate to a NSString
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"cccc, MMM d, hh:mm aa"];
NSString *dateString = [dateFormat stringFromDate:date];

// save to Parse
PFObject *addValues= [PFObject objectWithClassName:@"your-class"];
[addValues setObject: dateString forKey:@"your-key"];
[addValues saveInBackground];

I hope it helps!

I gave it a shot and this is error I got Error: invalid type for key selectedtime, expected date, but got string (Code: 111, Version: 1.2.19)

Make sure you are passing an NSDate here:

NSString *dateString = [dateFormat stringFromDate:date]; // date should be an NSDate object

Could you maybe post the code here?

Then I deleted the column out of parse,then tried the action again and this was the error Error: object not found for update (Code: 101, Version: 1.2.19)

Here is my code: PFUser *user =[PFUser currentUser]; NSDate *time = [self.timePicker date]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"cccc, MMM d, hh:mm aa"]; NSString *dateString = [dateFormat stringFromDate:time]; NSString *selectedGym = gymLabel.text;

[[PFUser currentUser] setObject:dateString forKey:@"selectedtime"];
[[PFUser currentUser] setObject:selectedGym forKey:@"selectedGym"];
[[PFUser currentUser] saveInBackground];

. PFUser *user =[PFUser currentUser];

NSDate *time = [self.timePicker date];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"cccc, MMM d, hh:mm aa"];

NSString *dateString = [dateFormat stringFromDate:time];
NSString *selectedGym = gymLabel.text;

[[PFUser currentUser] setObject:dateString forKey:@"selectedtime"];
[[PFUser currentUser] setObject:selectedGym forKey:@"selectedGym"];
[[PFUser currentUser] saveInBackground];

Your errors are looking like something Parse would throw. Make selectedTime is a String column on your table (inside your Data Browser) and not Date. If it the column is expecting a Date, you can simply store your NSDate there, like this:

PFUser *user =[PFUser currentUser];
NSDate *time = [self.timePicker date];

NSString *selectedGym = gymLabel.text;

[[PFUser currentUser] setObject:time forKey:@"selectedTime"];
[[PFUser currentUser] setObject:selectedGym forKey:@"selectedGym"];
[[PFUser currentUser] saveInBackground];

I still get an error

The same error ? :/

Error: object not found for update (Code: 101, Version: 1.2.19)