简体   繁体   English

如何在Swift 3中比较日期数组(字符串)和单个日期

[英]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. 在这里,我在表视图中显示TotalDaysServerArray数据。

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. 因此,在这里,如果用户按下第一个像22/11/2017 11:59:59这样的单元格,我需要检查dateFromDBArray中是否存在该日期(不是时间,只有相同的日期),如果存在,则需要检查多少索引本身已经存在,并且需要获取ActionsDBArray相同的索引数据。

So, I need to get the list of ActionsDBArray indexes and need to show the list of that in some other place. 因此,我需要获取ActionsDBArray索引的列表,并需要在其他地方显示该列表。

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 注意:我在04/12/2017 07:10:41手动添加了First对象以测试逻辑

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 2017年4月12日11:59:59

Optional(0) 可选(0)

Your Object in ActionsDBArray 1 ActionsDBArray 1中的对象

Hope it is helpful to you 希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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