Swift 快速检查两个数组是否在同一天没有循环
我有两个数组Swift 快速检查两个数组是否在同一天没有循环,swift,date,Swift,Date,我有两个数组[Date] 首先是使用myDates:[Date] 其次是使用coreDataDates:[Date] 我将用核心数据日期循环第二个数组 如何在循环中检查coreDataDates中是否存在myDates的天 我知道,我可以使用函数NSCalendar isDate(u:inSameDayAs:),但是在coreDataDates:[Date]循环中是否有一个函数我不需要循环myDates:[Date] 新手示例: for date1 in coreDataDates{ fo
[Date]
首先是使用myDates:[Date]
其次是使用coreDataDates:[Date]
我将用核心数据日期循环第二个数组
如何在循环中检查coreDataDates
中是否存在myDates
的天
我知道,我可以使用函数NSCalendar isDate(u:inSameDayAs:)
,但是在coreDataDates:[Date]
循环中是否有一个函数我不需要循环myDates:[Date]
新手示例:
for date1 in coreDataDates{
for date2 in myDates{
if(Calendar.current.isDate(date1, inSameDayAs: date2)==true){
//have it
}else{
//don't have it
}
}
}
我需要不必使用循环中循环的代码,比如:
let date1 = Date(timeIntervalSince1970: 1544019937)
let date2 = Date(timeIntervalSince1970: 1544019936)
let date3 = Date(timeIntervalSince1970: 1544019935)
let date4 = Date(timeIntervalSince1970: 1544019934)
let date5 = Date(timeIntervalSince1970: 1544019933)
let date6 = Date(timeIntervalSince1970: 1544019932)
let arr1 = [date1, date2, date3]
let arr2 = [date4, date5, date6]
print(arr2.filter { arr1.contains($0) }.count > 0 ) // false
let arr1 = [date1, date2, date3]
let arr2 = [date1, date5, date6]
print(arr2.filter { arr1.contains($0) }.count > 0 ) // true
比如:
let date1 = Date(timeIntervalSince1970: 1544019937)
let date2 = Date(timeIntervalSince1970: 1544019936)
let date3 = Date(timeIntervalSince1970: 1544019935)
let date4 = Date(timeIntervalSince1970: 1544019934)
let date5 = Date(timeIntervalSince1970: 1544019933)
let date6 = Date(timeIntervalSince1970: 1544019932)
let arr1 = [date1, date2, date3]
let arr2 = [date4, date5, date6]
print(arr2.filter { arr1.contains($0) }.count > 0 ) // false
let arr1 = [date1, date2, date3]
let arr2 = [date1, date5, date6]
print(arr2.filter { arr1.contains($0) }.count > 0 ) // true
您可以使用Set类的强大功能
let dateSet = Set(myDates)
let common = dateSet.intersection(coreDataDates)
//If you want it as an array
let commonDatesArray = Array(common)
有关更多信息,请参阅该类的文档。您可以使用Set类的强大功能
let dateSet = Set(myDates)
let common = dateSet.intersection(coreDataDates)
//If you want it as an array
let commonDatesArray = Array(common)
有关更多信息,请参阅课程文档。如果您不在乎时间,苹果建议您将时间设置为中午,因为由于夏令时的时钟变化,每天都有中午,而不是午夜 将两个数组中的所有日期转换为正午,然后找到两个集合的并集:
import Foundation
extension Date {
var noon: Date {
return Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
}
}
let myDates = [
DateComponents(calendar: .current, year: 2018, month: 12, day: 1, hour: 1).date!,
DateComponents(calendar: .current, year: 2018, month: 12, day: 2, hour: 2).date!,
DateComponents(calendar: .current, year: 2018, month: 12, day: 4, hour: 3).date!
]
let coreDataDates = [
DateComponents(calendar: .current, year: 2018, month: 12, day: 1, hour: 4).date!,
DateComponents(calendar: .current, year: 2018, month: 12, day: 3, hour: 5).date!,
DateComponents(calendar: .current, year: 2018, month: 12, day: 4, hour: 6).date!
]
let myDateSet = Set(myDates.map { $0.noon })
let coreDataDateSet = Set(coreDataDates.map { $0.noon })
let commonDateSet = myDateSet.intersection(coreDataDateSet)
print(commonDateSet)
如果你不在乎时间,苹果公司建议你将时间设置为中午,因为每天都有中午,而不是午夜,因为夏令时的时钟变化 将两个数组中的所有日期转换为正午,然后找到两个集合的并集:
import Foundation
extension Date {
var noon: Date {
return Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
}
}
let myDates = [
DateComponents(calendar: .current, year: 2018, month: 12, day: 1, hour: 1).date!,
DateComponents(calendar: .current, year: 2018, month: 12, day: 2, hour: 2).date!,
DateComponents(calendar: .current, year: 2018, month: 12, day: 4, hour: 3).date!
]
let coreDataDates = [
DateComponents(calendar: .current, year: 2018, month: 12, day: 1, hour: 4).date!,
DateComponents(calendar: .current, year: 2018, month: 12, day: 3, hour: 5).date!,
DateComponents(calendar: .current, year: 2018, month: 12, day: 4, hour: 6).date!
]
let myDateSet = Set(myDates.map { $0.noon })
let coreDataDateSet = Set(coreDataDates.map { $0.noon })
let commonDateSet = myDateSet.intersection(coreDataDateSet)
print(commonDateSet)
请清楚地解释您的问题。您可以使用收集方法
contains
,first(where:)
或index(where:)
/index(of:)
,如果找到元素(日期),这些方法将提前退出。@iDev750您不明白什么?我有一个带有日期的数组,我想检查是否每天都添加到数据库中。正如我所说,有两种方法,我想知道哪种方法对性能最好。我也不知道如何执行搜索。在我看来,这似乎是过早的优化。事实上,日期存储为双精度(timeIntervalSinceReferenceDate),每个日期仅使用4个字节。你说的是几次约会?我认为在处理日期时不会有任何性能问题。我列举了3个不同的问题,第一个是关于如何在核心数据中进行搜索,因为这样你就应该考虑使用谓词进行获取。请清楚地解释你的问题。你可以先使用收集方法包含,(其中:)
或索引(其中:)
/索引(of:)
如果找到元素(日期),这些将提供提前退出。@iDev750您不明白什么?我有一个带有日期的数组,我想检查是否每天都添加到数据库中。正如我所说,有两种方法,我想知道哪种方法对性能最好。我也不知道如何执行搜索。在我看来,这似乎是过早的优化。事实上,日期存储为双精度(timeIntervalSinceReferenceDate),每个日期仅使用4个字节。你说的是几次约会?我不认为在处理日期时会有任何性能问题。我列举了3个不同的问题,第一个是关于如何在核心数据中进行搜索的问题,因为接下来您应该研究使用谓词进行获取。