swift : get the closest date stored in the core data

跟風遠走 提交于 2019-12-25 14:32:09

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/34718367/swift-get-the-closest-date-stored-in-the-core-data

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