Unable to retrieve updatedAt or createdAt values from Parse objects

你说的曾经没有我的故事 提交于 2021-02-06 10:14:06

问题


I'm an experienced Android developer trying to get a prototype iOS app running using the Parse service and sdk (https://www.parse.com/).

It's great, and i can get all my objects and their values with no trouble, everything works fine.

However, i cannot get the updatedAt value automatically created by Parse for each object.
It's a must for me, and I dont want to have to save an aditional timestamp as a String when the data is sitting right there.

This is the gist of what i was doing.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}  

// this works fine 
cell.textLabel.text = [object objectForKey:@"name"];

//substring however does not
NSDate *updated = [object objectForKey:@"updatedAt"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEE, MMM d, h:mm a"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"Lasted Updated: %@", [dateFormat stringFromDate:update]];

//i also tried like this at first
 cell.detailTextLabel.text = [NSString stringWithFormat:@"Lasted Updated: %@", [object objectForKey:@"updatedAt"]];

return cell;

}


回答1:


Silly Matt. I think about this for a day then realise my mistake minutes after i post it.

updatedAt is a property on all PFObjects, no need to retrieve it using a key.

Given a PFObject named object...

 NSDate *updated = [object updatedAt];
 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
 [dateFormat setDateFormat:@"EEE, MMM d, h:mm a"];
 cell.detailTextLabel.text = [NSString stringWithFormat:@"Lasted Updated: %@", [dateFormat stringFromDate:updated]];

Working Date String

@Parse , tip of the hat




回答2:


For Swift:

let dateUpdated = object.updatedAt! as NSDate
let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "EEE, MMM d, h:mm a"
cell.updatedAtLabel.text = NSString(format: "%@", dateFormat.stringFromDate(dateUpdated))



回答3:


I just had the same problem, don't access it using objectForKey, just access it directly via the createdAt property.

in Swift

object.createdAt

As stated in the docs, the keys:

This do not include createdAt, updatedAt, authData, or objectId. It does include things like username and ACL.



来源:https://stackoverflow.com/questions/18425537/unable-to-retrieve-updatedat-or-createdat-values-from-parse-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!