How to query and convert Parse.com Date to NSDate/

2019-09-05 07:42发布

问题:

I want to take the parse column "createdAt", query it and make it into a string that displays on the UI as a label to show the user when a product was posted and when it expires.

There are similar questions on stackoverflow, but none have helped me and all have given me errors and crashes.

I am using a TableViewController class and I'm querying 2 things: the productName and the timeCreated. (The productName I'm able to display in a cell without a problem.)

I've tried several ways of getting the date:

Here is the first way. I tried querying it WITH the other Parse object:

var productName = [String]()

var timeCreated = [NSDate]()

//Arrays above the viewDidLoad() to store the productName and the   timeCreated

override func viewDidLoad() {
    super.viewDidLoad()



    var query = PFQuery(className: "ProductInfo")

    query.findObjectsInBackgroundWithBlock ({ (objects, error) -> Void in

    if let objects = objects {

        self.productName.removeAll(keepCapacity: true)
        self.timeCreated.removeAll(keepCapacity: true)

        for object in objects {

            self.productName.append(object["pName"] as! String)

            self.timeCreated.append(object["createdAt"] as! NSDate)



            print(self.productName)
            print(self.timeCreated)

            self.tableView.reloadData()


            }


        }


    })


}

The second way I'm trying is to query it separately from the productName object on its own, using this code. Ultimately formatting it, and then converting it into a string with stringFromDate. Im not putting it into an array, but I'm assuming I should as well if i want to use it?? This is the link I tried but didn't help: How to convert a parse date object to a string? Like this:

 var dateQuery = PFQuery(className:"ProductInfo")

    dateQuery.findObjectsInBackgroundWithBlock ({ (objects, error) -> Void in

        if let objects = objects {

        for object in objects {
            var createdAt = object["createdAt"]
            if createdAt != nil {
                let dateFormatter = NSDateFormatter()
                dateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"
                let date = dateFormatter.stringFromDate(createdAt as! NSDate)
                print(date)
            }
        }
    }
})

This is also in the viewDidLoad method, but just below the first query.

The error I'm getting is this: fatal error: unexpectedly found nil while unwrapping an Optional value for both ways.

It highlights this line in the first way I tried: self.timeCreated.append(object["createdAt"] as! NSDate)

and this line for the 2nd way I did it: let date = dateFormatter.stringFromDate(createdAt as! NSDate)

Why is it giving me an error? Any ideas on who to query it and convert it into a string (in an array) that I can use?

Any help is greatly appreciated.

回答1:

The created at date is like a private parameter of the PFObject, so you should use the accessor function provided rather than try to access it like a generic container. So, change your code to:

self.timeCreated.append(object.createdAt as NSDate)


回答2:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM'-'dd'-'yyyy'"

let date = dateFormatter.stringFromDate((<yourParseObjectHere>.createdAt as NSDate?)!)
yourLabel.text = date