快速比较两个数组的内容

时间:2019-07-28 18:40:36

标签: arrays swift

我必须在前3、7或30个条目上剪切一个数组。

然后我有一个ArraySlice,我想打包成一个数组。

现在,如果我想将我从ArraySlice创建的新数组与另一个数组(如果两个数组的内容相同)进行比较,我总是得到错误的结果。

我相信这是由于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]

2 个答案:

答案 0 :(得分:1)

解决此问题的一种方法是为日期提取一些日期成分,然后进行比较。

下面的代码在直接比较日期时将失败,但是在比较从年份到秒的各个分量时将成功

   timeline                events    dply-asm  ride-dply  cleaning-dply cleans  
    2018-01-19 08:18:05     BIKE_ASM    38       4            0            8
    2018-02-25 06:36:52     ride                
    2018-02-26 00:00:00     BIKE_DPLY
    2018-02-26 08:34:56     cleaning
    2018-03-02 13:32:00     ride
    2018-03-06 13:38:42     cleaning

为了使代码更清晰,可以将组件集设为常量

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

与此类似的解决方案可能是使用DateFormatter并比较字符串。两者都可以轻松设置比较日期时使用的精度

这是使用DateFormatter的一种方法,但是在此示例中,比较仅进行了几分钟即可完成

let compareSet:Set<Calendar.Component> = [.year, .month, .day, .hour, .minute, .second]

当然,对于任何一种解决方案,都无法直接比较数组,而是需要对其进行循环,并且需要将每个组件分别与另一个数组中的组件进行比较

下面是使用日期格式化程序解决方案的示例

let df = DateFormatter()
df.dateStyle = .full
df.timeStyle = .short
print(df.string(from: now) == df.string(from: now2))

答案 1 :(得分:0)

由于代码很棘手,并且您说它不应该工作,所以我发现您可以解决此问题:

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。