简体   繁体   中英

How to compare array of dates (Strings) with single date in swift 3

I have array of dates (strings) which are coming from local data base like following.

datesFromDBArray:["04/12/2017 07:10:41", "04/12/2017 07:12:17", "04/12/2017 07:13:54", "04/12/2017 07:17:45", "04/12/2017 07:18:56", "05/12/2017"]

Here, same date can have multiple times.

Also from same data base, I am getting some other data called actions.

ActionsDBArray:["1", "6", "1", "1", "1", "2", "2", "2", "4", "1", "5", "2", "3"]

these above two data getting from local database. And both arrays number of count is equal.

Now, I am getting number of dates from server response. That is like

TotalDaysServerArray:["22/11/2017 11:59:59", "23/11/2017 11:59:58", "24/11/2017 11:59:57"]

Here, I am showing TotalDaysServerArray data in table view.

So, here, If user pressed on first cell like 22/11/2017 11:59:59, I need to check this date (not time, only same date) is existed in datesFromDBArray, if existed, then need to check how many indexes its existed, and need to fetch ActionsDBArray same indexes data.

So, I need to get the list of ActionsDBArray indexes and need to show the list of that in some other place.

I have tried some logic which was not worked, so, I am posting query here.

Can anyone suggest me, how to achieve this?

let clickedStr = TotalDaysServerArray[indexPath.row] 
let str =  clickedStr.components(separatedBy: " ").first
 for(index , value) in datesFromDBArray.enumerated() {
            if str == value.components(separatedBy: " ").first! {
                print(ActionsDBArray[index])
            }
       }

It's child's play , Don't know where you stuck in logic

Note: I have added First object manually in 04/12/2017 07:10:41 to test the logic

var datesFromDBArray = ["04/12/2017 07:10:41", "04/12/2017 07:12:17", "04/12/2017 07:13:54", "04/12/2017 07:17:45", "04/12/2017 07:18:56", "05/12/2017"]
var ActionsDBArray = ["1", "6", "1", "1", "1", "2", "2", "2", "4", "1", "5", "2", "3"]
var TotalDaysServerArray = ["04/12/2017 11:59:59","22/11/2017 11:59:59", "23/11/2017 11:59:58", "24/11/2017 11:59:57"]

let findingValue =  TotalDaysServerArray.first!

print(findingValue)


let index = datesFromDBArray.index { (string) -> Bool in
    string.components(separatedBy: " ").first == findingValue.components(separatedBy: " ").first
}
print(index)



if let i = index {
    print("Your Object in ActionsDBArray \(ActionsDBArray[i])")
 }

Output :
04/12/2017 11:59:59

Optional(0)

Your Object in ActionsDBArray 1

Hope it is helpful to you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM