1

I want to add UIlable that represent the time when post created I tried 3 different methods without any success

PS: Am using PFQueryTableViewController

Update :

 var dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "MM-dd-yyyy HH:mm"
        var useDate = dateFormatter.stringFromDate
        cell.dateLabel.text = useDate

After above Code I get this error > can't assign of type NSDate -> string to type String? the error is points to cell.dateLabel.text = useDate

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject ? ) - > PFTableViewCell ? 
{
  let cell = tableView.dequeueReusableCellWithIdentifier("cellImg", forIndexPath: indexPath) as!MoreCell

  //Trial 1 
  cell.dateLabel.text = object ? .objectForKey(createdAt)

  //Trial 2
  cell.dateLabel.text = object ? .objectForKey("createdAt") as ? String

  //Trial 3
  cell.dateLabel.text = object ? .createdAt
 //Trial 4
  cell.dateLabel.text = object?.updatedAt >> Error Can't assign value of type NSDate to type string?

}

1 Answer 1

1

The error message is pretty self explanatory. The updatedAt token in Parse is a Date object, while you're trying to assign it to something that holds strings. So you need to change the date into a string before trying to set it for your text label.

EDIT:

You need to use your date formatter like this:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy HH:mm"
let useDate = dateFormatter.stringFromDate(object["updatedAt"] as! NSDate)
cell.dateLabel.text = useDate
Sign up to request clarification or add additional context in comments.

4 Comments

Crash > this line > let useDate = dateFormatter.stringFromDate(object!["updatedAt"] as! NSDate) >>fatal error: unexpectedly found nil while unwrapping an Optional value
Xcode force me to add ! after stringFromDate(object
check if exists first, if let date = object["updatedAt "] { ... }
can you explain how to check if it is there or not ? if let date = object["updatedAt "]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.