繁体   English   中英

如何比较两个数组的元素

[英]How to compare elements of two arrays

我想比较两个数组的元素,并检查它们是否相等。 我已经尝试了各种解决方案但没有真正有效。

我试过如何比较两个对象数组的解决方案

这是我的目标:

struct AccountBalance: Decodable {
    let balance: Double
    let currency: String

    init(balance: Double, currency: String ) {
        self.currency = currency
        self.balance = balance
    }

    enum CodingKeys: String, CodingKey {
        case currency = "Currency"
        case balance = "Balance"
    }
}

这是我试过的链接中的代码:

let result = zip(accountBalance, getsSaved).enumerate().filter() {
                $1.0 == $1.1
                }.map{$0.0}

但我得到这个错误:

Closure tuple parameter '(offset: Int, element: (AccountBalance, AccountBalance))' does not support destructuring with implicit parameters

Array提供了一个函数elementsEqual ,它能够比较两个数组而不显式符合Equatable

let result = accountBalance.elementsEqual(getsSaved) {
    $0.balance == $1.balance && $0.currency == $1.currency
}

编辑:

如果要获得相等结果而不管数组中对象的顺序如何,那么您只需为每个数组添加排序。

let result = accountBalance.sorted { $0.balance < $1.balance }.elementsEqual(getsSaved.sorted { $0.balance < $1.balance }) {
    $0.balance == $1.balance && $0.currency == $1.currency
}

我猜两个数组在包含相同元素时应该被认为是相等的,无论排序如何

首先,实现EquatableHashable

我使用hashValue作为id,所以我可以先对数组进行排序。

以下是您的AccountBalance类应如下所示:

struct AccountBalance: Decodable, Equatable, Hashable {


   // Important parts!
    var hashValue: Int{
        return balance.hashValue ^ currency.hashValue &* 1677619
    }
    static func == (lhs: AccountBalance, rhs: AccountBalance)  -> Bool{
        return lhs.balance == rhs.balance && lhs.currency == rhs.currency
    }

}

然后创建一个算法,对ararys进行排序,然后逐个检查每个元素是否内容相同。

这是使用EquatableHashable

func isEqual(arr1: [AccountBalance], arr2: [AccountBalance]) -> Bool{

    if arr1.count != arr1.count{
        return false
    }

    let a = arr1.sorted(){
        $0.hashValue > $1.hashValue
    }

    let b = arr2.sorted(){
        $0.hashValue > $1.hashValue
    }

    let result = zip(a, b).enumerated().filter() {
        $1.0 == $1.1
        }.count

    if result == a.count{
        return true
    }

    return false
}

我不确定你的其余代码是做什么的,但是让参数显式化会让XCode感到满意:

let result = zip(accountBalance, getsSaved).enumerated().filter() { (arg) -> Bool in
    let (_, (balance1, balance2)) = arg     
    return balance1.balance == balance2.balance
}.map{ $0.0 }`

试过(_, (balance1, balance2)) -> Bool in直接,但它也不会让我

我建议你实现你提供的链接的已接受答案,因为它控制两个数组的大小相同并且命令它们。

但如果你想要你的代码工作,我解决了这样的:

在此输入图像描述

为了控制比较,你的struct应该实现Equatable协议并重载operator ==

extension AccountBalance : Equatable {}

func ==(lhs: AccountBalance, rhs: AccountBalance) -> Bool {
    return lhs.balance == rhs.balance && lhs.currency == rhs.currency
}

然后比较两个数组并检查是否包含false,如果包含false,则数组中的一个或多个项不相同。

let result = !zip(accountBalance, getsSaved).enumerated().map() {
    $1.0 == $1.1
}.contains(false)

希望它能帮到你

有几个数组的示例:

import Foundation

let arr1: [String?] = ["word", nil, "word3", "word4"]
let arr2: [Double?] = [1.01, 1.02, nil, 1.04]
let arr3: [Int?] = [nil, 2, 3, 4]

var tuple1: [(title: String, number: String, coord: String)] = []

let countArray = arr1.count

for element in 0..<countArray {
    tuple1.append((arr1[element].map 
{String($0)} ?? "", arr2[element].map 
{String($0)} ?? "", arr3[element].map 
{String($0)} ?? ""))
}

print(tuple1)

印刷结果:

[(title: "word1", number: "1.01", coord: ""), (title: "", number: "1.02", coord: "2"), (title: "word3", number: "", coord: "3"), (title: "word4", number: "1.04", coord: "4")]

暂无
暂无

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

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