0

I'm creating a school app that displays the messages, users and the timestamp. I tried looking online for a fix to this but i couldn't find anything that I could understand. I'm new to programming and anything could help. I read somewhere that NSLog could help but I don't know how it's used. Please help!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"messagesCell";
    MessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    reversedMessages = [[messagesArray reverseObjectEnumerator] allObjects];

    PFObject *tempObject = [reversedMessages objectAtIndex:indexPath.row];

    cell.cellTitle.text = [tempObject objectForKey:@"content"];
    cell.userName.text = [tempObject objectForKey:@"user"];

    NSDate *createdAt = [tempObject createdAt];
    NSDateFormatter *dateDisplay = [[NSDateFormatter alloc] init];
    [dateDisplay setDateFormat:@"MMM, d h:mm a"];

    **cell.timeStamp.text = [NSString stringWithFormat: [dateDisplay stringFromDate:createdAt]];**

    return cell;
}
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
  • 5
    This makes no sense: `[NSString stringWithFormat: [dateDisplay stringFromDate:createdAt]]` -- There is no sense in using `stringWithFormat` when there's no replacement text. – Hot Licks Aug 23 '14 at 02:32
  • And if you are truly "new to programming" you *should not* start with Objective-C. You need to learn C or Java first, or there are many concepts you will never "get". – Hot Licks Aug 23 '14 at 02:35
  • What is "(Potentially insecure)"? – zaph Aug 23 '14 at 02:41
  • 1
    @Zaph - That's a compiler warning that you get if you pass a string variable as the format parm of stringWithFormat or NSLog. The reasoning is that you could have the moral equivalent of a SQL injection exposure. – Hot Licks Aug 23 '14 at 02:48
  • I did not realize that the title was an error message, I see that now. – zaph Aug 23 '14 at 02:54

1 Answers1

1

If you're using stringWithFormat, it expects a format string with %@'s being replaced by comma-separated parameters, like this:

[NSString stringWithFormat:@"This is param1: %@, and param2: %@", param1, param2];

If you're using a normal NSString, you want to simply do this:

cell.timeStamp.text = [dateDisplay stringFromDate:createdAt];

The alternative, which I want to doubly emphasize is just to contrast with the better way above and in this case is pointless, would be:

cell.timeStamp.text = [NSString stringWithFormat:@"%@", [dateDisplay stringFromDate:createdAt]];
Mike
  • 9,765
  • 5
  • 34
  • 59