簡體   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