快速比较两个结构数组

时间:2021-01-15 14:38:26

标签: arrays swift codable

通过网络下载的 JSON 数据被转换为符合 codable 的结构体。所有的数据都保存在一个结构体数组中。有一些重复的数据来自 api。我想限制重复数据添加到数组中。但是我遇到了一些错误。

我的结构是

struct LcodeData: Codable {
    let name: String?
    let lcodeID: String?
    let defaultJustification, standardCost: String?
    let startPage: String?
        let nextPage: Int?

    enum CodingKeys: String, CodingKey {
        case name = "lcode_name"
        case lcodeID = "lcode_id"
        case defaultJustification = "default_justification"
        case standardCost = "standard_cost"
        case startPage = "start_page"
       case nextPage = "next_page"
    }
}

enum Name: String, Codable {
    case a6549 = "A6549"
    case a9270 = "A9270"
    case e1399 = "e1399"
    case l0999 = "L0999"
    case l1499 = "L1499"
    case l2999 = "L2999"
    case l3649 = "L3649"
    case l3999 = "L3999"
    case l4205 = "L4205"
    case l4210 = "L4210"
    case l5999 = "L5999"
    case l7499 = "L7499"
    case l7510 = "L7510"
    case l7520 = "L7520"
    case l8499 = "L8499"
    case nameE1399 = "E1399"
}

typealias Empty = [LcodeData]

我的api调用:-

  let spinner = showLoader(view: self.view)
        apiService.lcodeApiWithPagination(pageNumber: currentPage, recordsPerPage: 20) { [self] (lcodeData) in
            
            
            print("lcodeData is \(lcodeData?.count)")
            if let lcde = lcodeData{
                
                if lcde.count > 0{
                    
                    let filteredArray = lcde.filter{!contains(allLcodeArray, $0) }

                    
                    DispatchQueue.main.async {
                        self.tableView.reloadData()
                    }
                    

                }
            }
           spinner.dismissLoader()
        }

我的 api 响应是:-

[
    {
        "lcode_id": "2",
        "lcode_name": "L0124",
        "default_justification": "We don’t see things as they are, we see them as we are.",
        "standard_cost": "50.0000"
    },
    {
        "lcode_id": "3",
        "lcode_name": "L1234",
        "default_justification": "Remember that failure is an event, not a person.",
        "standard_cost": "50.0000"
    },
    {
        "lcode_id": "4",
        "lcode_name": "L5678",
        "default_justification": "If you wait, all that happens is you get older.",
        "standard_cost": "35.0000"
    },
    {
        "lcode_id": "5",
        "lcode_name": "L9903",
        "default_justification": "All journeys have secret destinations of which the traveler is unaware.",
        "standard_cost": "60.0000"
    }
    ]
var allLcodeArray = [LcodeData]()
Here i am storing all the json response in "allLcodeArray"

当我尝试时:-

let filteredArray = lcde.filter{!contains(allLcodeArray, $0) }

我收到类似错误 - “参数类型‘[LcodeData]’不符合预期类型‘UIFocusEnvironment’”

0 个答案:

没有答案