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

限于喜欢 提交于 2020-01-06 08:35:08

问题


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


来源:https://stackoverflow.com/questions/33329932/how-to-query-and-convert-parse-com-date-to-nsdate-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!