mur7ay
mur7ay

Reputation: 833

Displaying The Date with NSDateFormatter

What I'm trying to do is display the date and time somewhat similar to what you would see within Facebook. However, Xcode is displaying this error:

use of unresolved identifier 'creator'.

Here's my function I'm having trouble with:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("PostCell", forIndexPath: indexPath) as! PostCell
    cell.post = posts[indexPath.row]
    cell.index = indexPath.row

    var dataFormatter:NSDateFormatter = NSDateFormatter()
    dataFormatter.dateFormat = "yyyy-MM-dd HH:mm"
    cell.timestampLabel.text = dataFormatter.stringFromDate(creator.createdAt)

    return cell
}

An error wasn't displaying until I typed out the second to last line of code. Specifically, I know the error is being caused by creator, but I also believe that timestampLabel is the culprit also because it didn't change a color like the person I was following along with in the video. Now I'm utilizing Parse to store all the users data and creator is one of the categories I have in Parse. I was hoping someone could point me in the right direction as I've spent way to long on something that's probably really simple.

Upvotes: 1

Views: 466

Answers (1)

ipraba
ipraba

Reputation: 16543

The culprit is not the timestampLabel. The culprit is the creator object.

use of unresolved identifier 'creator'

means the object is not defined in the current scope. In your case creator object. creator should be defined.

Assuming from the youtube video and your code posts object is an array of parse objects(PFObject)

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("PostCell", forIndexPath: indexPath) as! PostCell
    cell.post = posts[indexPath.row]
    cell.index = indexPath.row
    //---------------------------Add this-------------------
    var creator = cell.post as PFObject
    //------------------------------------------------------

    var dataFormatter:NSDateFormatter = NSDateFormatter()
    dataFormatter.dateFormat = "yyyy-MM-dd HH:mm"
    cell.timestampLabel.text = dataFormatter.stringFromDate(creator.createdAt)


    return cell
}

Upvotes: 3

Related Questions