last day (14 days later) » 

7:27 PM
0
A: How to get the closest time to current time from an array of dates ? iOS, Swift 3

George GreenYou can simply sort your list of dates based on it's offset from the current time, using a calendar and date components we can make sure we only look at the hour and minute components of the date: var dates = [Date]() // assume somehow this array is populated let cal = Calendar.current let nowCo...

 
@LucaD'Alberti You are absolutely right, I misread the question :(
⬆ Answer updated to actually answer the question 🤦‍♂️
 
@GeorgeGreen thanks. I wish I had clarified my question more. By the closest time I meant that if we have for example current time 12:23 PM and it the array there is 6:48AM, 12:05 PM and 4:01AM then closest time to that time would be 4:01 AM, so that the closest time is calculated "forward", not backwards.
 
@tommagellancook Ah, so what you want is the "next" time from now, in the array?
@tommagellancook See updated answer
 
@GeorgeGreen I'm impressed with the use of of the tuple. I have run the code however the result is not what is expected. I need to only consider the time (hours and minutes) and disregard the rest of the date parts.
 
@tommagellancook Sorry about that, I forgot that we don't want the difference between the two, but in fact the difference in absolute hours from the day's origin. See updated for a hopefully working solution :)
 
7:27 PM
@GeorgeGreen First of all thanks. Could you run your code in a playground against my sample data from the question, and as a current date use the currentDate provided in the sample? What are your results?
 
@tommagellancook With your test data this correctly selects March 1st 0030hrs (third date)?
 
I must have done something wrong. I will try again in the playground.
 
I can't download but I've tried again in a playground and it works! :)
How did you come up with the algorithm? (on paper?) How long are you programming in iOS?
(the download says access denined. 403 Forbidden)
 
Ah yes, I sent you the wrong link, my bad.
No, just in SO, have been programming iOS since 2008 when the app store began, so know my way around most of it
Are you new coming to iOS?
 
7:40 PM
Yes, I have about 1 year of experience but a bit scattered between iOS, iBeacons and tvOS. Thanks for helping me out, I was stuck after getting the requested date objects from CoreData. I really liked how you came up with the algorithm and used the functional part of Swift and the trailing closures :)
Is it possible to add friends to a "friends list" in SO?
 
8:28 PM
Hmmm, I'm not sure that it is, though it would be quite a nice feature for building little groups of trusted techies and friends
 

last day (14 days later) »