7

For example:

  • stored data: date = data1(2015.12.31), data2(2016.01.05), data3(2016.01.14), data4(2016.01.15), data5(2016.01.18)
  • today is 2016.01.12
  • then nearest data is data3

What are the methods that can be called here data3?

realDate(datePicker.date), date(textField.text), content(textField.text) in Core Data.

var stores: Contact!
// Contact is CoreData Entity Name.
@IBOutlet weak var label: UILabel!

ViewController viewDidLoad :

let list = [stores]
let timeSort = list.map{_ in stores?.realDate!.timeIntervalSinceNow}.filter{$0 > 0}.sort(<)
  if let firstTime = timeSort.first {
  _ = NSDate(timeIntervalSinceReferenceDate: firstTime!)
    if timeSort.first != nil {
      label.text = stores?.content
    } else { }
  }

I was trying to get help for writing code. There is no error, it does not display any information on the label.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Iden Lim
  • 117
  • 2
  • 7
  • Where is the difference to your previous question http://stackoverflow.com/questions/34718367/swift-get-the-closest-date-stored-in-the-core-data? If the answers does not satisfy you, ask the author for clarification, and add more information to the question. But don't simply repeat it. – Martin R Jan 11 '16 at 17:22
  • Martin R. I understand. thank you. – Iden Lim Jan 11 '16 at 22:43

1 Answers1

13

You can use NSDate property timeIntervalSinceNow to sort your dates and get the first positive value:

edit/update:

Swift 3 introduced Date which conforms to Comparable protocol so it can be easily sorted now:

extension Date {
    init?(_ year: Int,_ month: Int,_ day: Int) {
        guard let date = DateComponents(calendar: .current, year: year, month: month, day: day, hour: 12).date else { return nil }
        self = date
    }
}

Playground testing:

let dateList = [Date(2018, 2, 18)!, Date(2017, 12, 31)!, Date(2018, 1, 5)!, Date(2018, 2, 14)!, Date(2018, 2, 15)!]

if let closestDate = dateList.sorted().first(where: {$0.timeIntervalSinceNow > 0}) { // "Feb 15, 2018, 12:00 PM"
    print(closestDate.description(with: .current)) // Thursday, February 15, 2018 at 12:00:00 PM Brasilia Summer Time
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • 1
    Your advice was very helpful. Thank you for your help. – Iden Lim Jan 11 '16 at 22:46
  • @IdenLim If this answer helped you, then you should accept it by clicking on the tick. – Beau Nouvelle Jan 12 '16 at 01:21
  • What is the meaning of 'map' in 'dateList.map'? – Iden Lim Jan 12 '16 at 02:48
  • 1
    @IdenLim it is used to iterate through each element (in this case NSDate) in your array and get the timeIntervalSinceNow property fro each date. With filter I get only the positive values from the result. The sort makes sure the first element it is the lowest time interval – Leo Dabus Jan 12 '16 at 02:54
  • @LeoDabus very very Thank you! Please review the additional questions once more. I hope you understand that I am beginner developer. Thank you. – Iden Lim Jan 12 '16 at 05:05
  • you code it is very confusing. I am not familiarised with core date so I can't understand what you are actually trying to do. Try playing with the code I provided in a playground to understand how to use map, filter and sort separately – Leo Dabus Jan 12 '16 at 05:12
  • @LeoDabus why this is excluding the current date?? what should i do to get the current date as the output if the current date exist in the date array. Please reply fast if you are online – Irshad Qureshi Apr 28 '16 at 10:27
  • 1
    Great answer @LeoDabus! My only issue was that by the time `timeSorted` was initialized and I initialized a new `Date` object based on the time interval, there was a minor time elapse and it altered the original date by a little. I resolved this by creating a `currentDate` before sorting and using `Date(timeInterval: interval, since: currentDate)`. – Natanel Feb 14 '18 at 21:02
  • 1
    @IrshadQureshi Sorry for the late reply I've just saw your comment. It will only exclude current date if your current date time it is already in the past. In my example I am setting all dates to 12pm (noon) so if I run this code after 12pm the current date at 12pm will be excluded (timeIntervalSinceNow is negative because the date it is already in the past) – Leo Dabus Feb 15 '18 at 02:37