13

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;

}

4

3 回答 3

51

愚蠢的马特。我想了一天,然后在发布后几分钟意识到我的错误。

updatedAt 是所有 PFObjects 的属性,无需使用键检索它。

给定一个 PFObject 命名对象...

 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]];

工作日期字符串

@Parse,帽子的一角

于 2013-08-25T04:12:27.163 回答
15

对于斯威夫特:

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))
于 2014-09-12T13:18:10.340 回答
2

我只是遇到了同样的问题,不要使用objectForKey访问它,只需通过createdAt属性直接访问它。

在斯威夫特

object.createdAt

如文档中所述,键:

这不包括 createdAt、updatedAt、authData 或 objectId。它确实包括用户名和 ACL 之类的内容。

于 2015-10-17T05:06:12.800 回答