[英]Compare the content of two arrays in swift
我必須在前3、7或30個條目上剪切一個數組。
然后我有一個ArraySlice,我想打包成一個數組。
現在,如果我想將我從ArraySlice創建的新數組與另一個數組(如果兩者都具有相同的內容)進行比較,我總是得到false。
我相信這是由於ArraySlice中新創建的Array引起的。
我應該怎么做才能使結果不總是錯誤的。 內容相同,已通過打印(...)進行了檢查
extension Date {
static func compareLastDays (compareArray: [Date]) -> Bool{
var compareArray2: [Date] = []
var createDateArray: [Date] = []
var days = compareArray.count
if days >= 30 {
days = 30
let arraySlice = compareArray.prefix(days)
compareArray2 = Array(arraySlice)
}
else if days >= 7{
days = 7
let arraySlice = compareArray.prefix(days)
compareArray2 = Array(arraySlice)
}
else {
days = 3
let arraySlice = compareArray.prefix(days)
compareArray2 = Array(arraySlice)
}
let startDate = Date.init()
var endDate = Calendar.current.date(byAdding: .day, value: -days, to: startDate)!
print("startDate", startDate)
print("endDate", endDate)
while startDate > endDate{
createDateArray.insert(endDate, at: 0)
guard let newDate = Calendar.current.date(byAdding: .day, value: 1, to: endDate) else {
break
}
print("extension new Date", newDate)
endDate = newDate
}
print(compareArray2, "extension compareArray")
print(createDateArray, "extension createDateArray")
if createDateArray == compareArray2 {
print("Compare ARRAY true", createDateArray, compareArray2)
return true
}
else {
print("Compare ARRAY false", createDateArray, compareArray2)
return false
}
}
}
打印報表:
startDate 2019-07-28 19:00:22 +0000
endDate 2019-07-21 19:00:22 +0000
extension new Date 2019-07-22 19:00:22 +0000
extension new Date 2019-07-23 19:00:22 +0000
extension new Date 2019-07-24 19:00:22 +0000
extension new Date 2019-07-25 19:00:22 +0000
extension new Date 2019-07-26 19:00:22 +0000
extension new Date 2019-07-27 19:00:22 +0000
extension new Date 2019-07-28 19:00:22 +0000
[2019-07-27 19:00:22 +0000, 2019-07-26 19:00:22 +0000, 2019-07-25 19:00:22 +0000, 2019-07-24 19:00:22 +0000, 2019-07-23 19:00:22 +0000, 2019-07-22 19:00:22 +0000, 2019-07-21 19:00:22 +0000] extension compareArray
[2019-07-27 19:00:22 +0000, 2019-07-26 19:00:22 +0000, 2019-07-25 19:00:22 +0000, 2019-07-24 19:00:22 +0000, 2019-07-23 19:00:22 +0000, 2019-07-22 19:00:22 +0000, 2019-07-21 19:00:22 +0000] extension createDateArray
Compare ARRAY false [2019-07-27 19:00:22 +0000, 2019-07-26 19:00:22 +0000, 2019-07-25 19:00:22 +0000, 2019-07-24 19:00:22 +0000, 2019-07-23 19:00:22 +0000, 2019-07-22 19:00:22 +0000, 2019-07-21 19:00:22 +0000] [2019-07-27 19:00:22 +0000, 2019-07-26 19:00:22 +0000, 2019-07-25 19:00:22 +0000, 2019-07-24 19:00:22 +0000, 2019-07-23 19:00:22 +0000, 2019-07-22 19:00:22 +0000, 2019-07-21 19:00:22 +0000]
解決此問題的一種方法是為日期提取一些日期成分,然后進行比較。
下面的代碼在直接比較日期時將失敗,但是在比較從年份到秒的組件時將成功
let now = Date()
let now2 = Date()
print(now == now2 ) // -> false
let components1 = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute, .second], from: now)
let components2 = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute, .second], from: now2)
print(components1 == components2) // -> true
為了使代碼更清晰,可以將組件集設為常量
let compareSet:Set<Calendar.Component> = [.year, .month, .day, .hour, .minute, .second]
與此類似的解決方案是使用DateFormatter並比較字符串。 兩者都可以輕松設置比較日期時使用的精度
這是使用DateFormatter的一種方法,但是在此示例中,比較僅需進行幾分鍾即可完成
let df = DateFormatter()
df.dateStyle = .full
df.timeStyle = .short
print(df.string(from: now) == df.string(from: now2))
當然,對於任何一種解決方案,都無法直接比較數組,而是需要對其進行循環,並且需要將每個組件分別與另一個數組中的組件進行比較。
以下是使用日期格式化程序解決方案的示例
guard compareArray2.count == createDateArray.count else { return false}
for i in 0..<compareArray2.count {
if df.string(from: compareArray2[i]) != df.string(from: createDateArray[i]) {
return false
}
}
return true
由於代碼很棘手,並且您說它不應該這樣做,所以我發現您可以解決此問題:
extension Array where Element: Hashable {
func isSame(from other: [Element]) -> Bool {
let thisSet = Set(self)
let otherSet = Set(other)
let arr = Array(thisSet.symmetricDifference(otherSet))
if arr.count == 0{
return true
}else{
return false
}
}
}
let date1 = Date()
let date2 = Date()
let date3 = Date()
let arr1 = [date1,date2,date3]
let arr2 = [date1,date2]
arr1.isSame(from: arr2)
如果兩個數組相同,則返回true,否則返回false。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.