如何查询并将Parse.com日期转换为NSDate/String [英] How to query and convert Parse.com Date to NSDate/String

查看:101
本文介绍了如何查询并将Parse.com日期转换为NSDate/String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要获取解析列"createdAt",对其进行查询并使其成为一个字符串,该字符串作为标签显示在UI上,以向用户显示产品何时发布以及何时过期.

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.

关于stackoverflow也有类似的问题,但是没有一个问题对我有帮助,所有人都给了我错误和崩溃的信息.

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

我正在使用TableViewController类,正在查询两件事:productNametimeCreated. (productName我可以毫无问题地显示在单元格中.)

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:

这是第一种方法.我尝试用另一个Parse对象查询它:

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()


            }


        }


    })


}

我尝试的第二种方法是使用此代码与productName对象单独进行查询.最终将其格式化,然后使用stringFromDate将其转换为字符串.我没有将其放入数组中,但是我假设如果我想使用它,我也应该这样做?这是我尝试但没有帮助的链接:如何将解析日期对象转换为字符串?像这样:

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)
            }
        }
    }
})

这也在viewDidLoad方法中,但仅在第一个查询下方.

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

我遇到的错误是:fatal error: unexpectedly found nil while unwrapping an Optional value两种方式.

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

它以我尝试的第一种方式突出显示了这一行: self.timeCreated.append(object["createdAt"] as! NSDate)

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

这是我做第二种方法的这一行: let date = dateFormatter.stringFromDate(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.

推荐答案

在日期创建的就像PFObject的私有参数一样,因此您应该使用提供的访问器函数,而不要像普通容器那样尝试访问它.因此,将代码更改为:

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)

这篇关于如何查询并将Parse.com日期转换为NSDate/String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆