Search code examples
swiftcore-datansdatanstimeinterval

swift : get the closest date stored in the core data


I have two data in core data.

  • Entity Name = "Contact", Attribute Name = "date"(selected datePicker.date), "content"(textFeild.text)

It is that sort of information in a table view, was a success. I want to show only one content in the another view controller. The data of the closest time to the current time.

I was asked to help with the following code.

var stores: Contact?

if ((stores?.date!.timeIntervalsince1970 ?? 0) > NSDate().timeIntervalSince1970) {

    labelName.text = stores?.content!
    }

But the label has not been any indication on. And no error... What's the problem?


Solution

  • As you mentioned using Core Data, the right direction is to Fetch this objects (the contacts). Then you will have an Array of NSManagedObjects. If the fecth is successful you iterate them in a ´for in´ loop. Inside this loop use ´.valueForKey´ to extract the dates and apply the rules you have created, returning the NsManagedObject or Atribute you want.