-1

I'm querying the createdAt column from Parse.

My Parse methods are above this code and then I'm doing this:

var createdAt = object.createdAt

if createdAt != nil {

    let twentyFourHours = NSTimeInterval(60 * 60 * 24)
    self.expiresAt = NSDate(timeInterval: twentyFourHours, sinceDate: (createdAt!!))

}

I am querying many dates from Parse. However, I'm unable to store them in createdAt because createdAt is of type NSDate?.

I need to make an array, but I can't figure out how to store many NSDate values so that I can compare them.

How can I store the values I'm querying and put them into an array so that I can compare many createdAt dates with the NSTimeInterval method using this method: NSDate(timeInterval: twentyFourHours, sinceDate: (createdAt!!))?

Am I using the correct function?

Thanks in advance.

Lukesivi
  • 2,206
  • 4
  • 25
  • 43

1 Answers1

1

I guess the easiest way is to store the data as Double

let date = NSDate().timeIntervalSince1970
let doubleDate = Double(date)

Create an array, store and very easy to compare. Than when you query it, just do:

var date = NSDate(timeIntervalSince1970: Double(dateDouble))

exemple:

    func exemple() {
    // SAVING
    // take you date
var array = [Double]()

    let dateToSave = NSDate().timeIntervalSince1970
    let dateToSave1 = NSDate().timeIntervalSince1970
    let dateToSave2 = NSDate().timeIntervalSince1970
    let dateToSave3 = NSDate().timeIntervalSince1970
    let dateToSave4 = NSDate().timeIntervalSince1970

    array.append(Double(dateToSave))
    array.append(Double(dateToSave1))
    array.append(Double(dateToSave2))
    array.append(Double(dateToSave3))
    array.append(Double(dateToSave4))
    print(array)
    let objectToSave = PFObject(className:"SomeDates")
    objectToSave["dates"] = array
    objectToSave.saveInBackgroundWithBlock {
        (success: Bool, error: NSError?) -> Void in
        if (success) {
            //done


        } else {
            // done != true :))
        }
    }
}

create a column of type array

i test it and it works!

  • Can you give an example of the array and how to store it? – Lukesivi Nov 04 '15 at 12:31
  • any ideas? Would appreciate an explanation for how to create the array. Thanks @RaduTataru – Lukesivi Nov 08 '15 at 09:04
  • Hi, your class in parse should have a column of type array, i edited my answer, so if you have in parse a column named "dates", and a class named "SomeDates" this code should work, i tested in my environment, ant it is ok. Create a class, and a column an run this function, you'll see the new object in parse whith all the dates stored as double value in one array – Radu Tataru Nov 08 '15 at 17:12
  • can you send me a gist? – Lukesivi Nov 08 '15 at 18:37