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
Mike Lee
6,579 PointsHow 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
Marcus Vieira
7,877 PointsHi 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!
Mike Lee
6,579 PointsI 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)
Marcus Vieira
7,877 PointsMake 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?
Mike Lee
6,579 PointsThen 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)
Mike Lee
6,579 PointsHere 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];
Mike Lee
6,579 Points. 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];
Marcus Vieira
7,877 PointsYour 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];
Mike Lee
6,579 PointsI still get an error
Marcus Vieira
7,877 PointsThe same error ? :/
Mike Lee
6,579 PointsError: object not found for update (Code: 101, Version: 1.2.19)