【问题标题】:Combine multiple search fields in swift快速组合多个搜索字段
【发布时间】:2021-02-10 20:04:18
【问题描述】:

我有四个必须搜索和过滤的字段。我可以单独搜索它,但现在我想组合所有 4 个字段并获取结果,然后重新加载集合视图。

使用的数据模型如下:

import Foundation

// MARK: - Empty
struct SearchDataModel: Codable,Equatable {
    
    static func == (lhs: SearchDataModel, rhs: SearchDataModel) -> Bool {
        return false
    }
    
    let status: Int
    var data: [Datum]
}

// MARK: - Datum
struct Datum: Codable,Equatable {
    let userID: Int
    let name: String
    let image: String?
  //  let image: JSONNull?
    let userAddress: String?
    let id: Int
    let listingImage: String?
    let listingVideo: String?
    let listingTitle: String
    let listingAddress: String?
    let listingPrice: String?
    let listingType: String
    let listingUse: String
    let listingSqft: String?
    let listingBedsCount, listingBathsCount, listingParkingSpot: Int?
    let listingDescription: String
    let featureListing: Int
    let createdAt, updatedAt: String

    enum CodingKeys: String, CodingKey {
        case userID = "user_id"
        case name, image
        case userAddress = "user_address"
        case id
        case listingImage = "listing_image"
        case listingVideo = "listing_video"
        case listingTitle = "listing_title"
        case listingAddress = "listing_address"
        case listingPrice = "listing_price"
        case listingType = "listing_type"
        case listingUse = "listing_use"
        case listingSqft = "listing_sqft"
        case listingBedsCount = "listing_beds_count"
        case listingBathsCount = "listing_baths_count"
        case listingParkingSpot = "listing_parking_spot"
        case listingDescription = "listing_description"
        case featureListing = "feature_listing"
        case createdAt = "created_at"
        case updatedAt = "updated_at"
    }
}

搜索的字段有listingprice、listingtype、listinguse、listingaddress。

每个字段的搜索过滤器如下:

let list1 = orgsearchDataModel?.data.filter { $0.listingAddress!.range(of: keyword!, options: .caseInsensitive) != nil }
let list2 = orgsearchDataModel?.data.filter { $0.listingType.range(of: type, options: .caseInsensitive) != nil }
   
let list3 = orgsearchDataModel?.data.filter { $0.listingUse.range(of: use, options: .caseInsensitive) != nil }
             
let list4 = orgsearchDataModel?.data.filter {
            if let price = Int($0.listingPrice ?? "0") {
                return (price > min! && price < max!)
            }
            return false
        }

现在得到所有四个的组合搜索结果如下:

let a = list1!.filter { list2!.contains($0) && list3!.contains($0) && list4!.contains($0)}  

问题是它正在将list1list2list3 结合起来,并且它可以正确显示结果,

let a = list1!.filter { list2!.contains($0) && list3!.contains($0) } 

,但是当我也添加list4 时,结果为零。

有人可以帮我如何组合所有搜索并获得一个满足所有四个搜索字段的结果。

【问题讨论】:

  • 您不需要合并 4 个列表。只需使用所有条件过滤 1 次。
  • 怎么做?基本上我需要所有 4 个过滤结果的共同元素。
  • 也许这个pastebin.com/T0rBJkqe
  • 可能列表 4 中的搜索已损坏。
  • @Larme 用哪一种?有两种方法

标签: swift search filter contains


【解决方案1】:

确认你的结构为 Hashable 并实现方法

struct Datum: Codable,Equatable, Hashable {
   func hash(into hasher: inout Hasher) {
        hasher.combine(userID)
        hasher.combine(name)
        hasher.combine(image)
        //Add other values too
    }
}

let result = Set(list1 ?? [])
    .intersection(list2 ?? [])
    .intersection(list3 ?? [])
    .intersection(list4 ?? [])

let array = Array(result)

【讨论】:

  • 那不会失去订单吗?
  • 是的,当您将元素添加到 set 时,不会保留顺序。您需要根据什么标准对列表进行排序。
  • @udbhateja 你是在向我要订单吗?
  • @saranya 更新了答案,请检查。
猜你喜欢
  • 2017-01-25
  • 1970-01-01
  • 1970-01-01
  • 2014-11-05
  • 1970-01-01
  • 1970-01-01
  • 2013-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多