0

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.

2
  • fatal error: unexpectedly found nil while unwrapping an Optional value: This was asked and answered a hundred times. It's basic Swift behaviour. If you don't know why this happens, you need to learn Swift. Commented Oct 25, 2015 at 12:59
  • I understand why it's happening. However, I don't know how to get rid of it and actually query the date. Thats my issue. Commented Oct 25, 2015 at 13:00

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

7 Comments

I get an error: `Cannot convert value of type 'NSDate?!" to expected argument type 'NSDate' I've tried down casting it as well.
It's a nullable property in obj-c, and technically it won't exist for explicitly created objects that aren't uploaded yet, but in your case it's safe to unpack
Yup, worked. thanks! How would I convert it now into a string?
Using an NSDateFormatter like you have, your issue was never formatting it was simply accessing the variable properly
small question: I'm getting a warning on the line you posted above on the as telling me to downcast. For now I made it as! NSDate! because if not, i an error. Anyway to get rid of the error? Ive tried various combinations.
|
1
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM'-'dd'-'yyyy'"

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

Comments

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.