微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从NSObject子数组数据中过滤NSObject数组?

如何解决如何从NSObject子数组数据中过滤NSObject数组?

我想从内部NSObject数组数据中过滤NSObject数组。这是我的杰森回复

{"success":true,"message":"Invoice list","payload":{"invoice_list":[{"date":"2020-10-20","invoices":[{"id":41,"client_name":"Client 001"},{"id":43,"client_name":"Client 003"}]},{"date":"2020-10-19","invoices":[{"id":37,{"id":42,"client_name":"Client 002"}]},{"date":"2020-10-14","invoices":[{"id":36,"client_name":"Client 004"}]}]},"code":200}

这是我的阵列

var arrInvoiceList: [BossInvoiceDateObject] = []

这是我的NSObject类

class BossInvoiceDateObject: NSObject {
    var date: String = ""
    var arrInvoices: [BossInvoiceObject] = []

    init(_ dictionary: [String: Any]) {
        self.date = dictionary["date"] as? String ?? ""
        
        if let invoices = dictionary["invoices"] as? [Dictionary<String,Any>] {
            for i in 0..<invoices.count  {
                let objInvoice = BossInvoiceObject.init(invoices[i])
                self.arrInvoices.append(objInvoice)
            }
        }
    }
}


class BossInvoiceObject: NSObject {
    var id: Int = 0
    var client_name: String = ""

    init(_ dictionary: [String: Any]) {
        self.id = dictionary["id"] as? Int ?? 0
        self.client_name = dictionary["client_name"] as? String ?? ""
    }
}

这是我的UI屏幕截图链接https://ibb.co/2j7ZDzb

现在我想从BossInvoiceObject类型的内部数组中搜索

假设我在搜索中键入“客户端001” ,那么它应该像下面的响应一样过滤结果。

{"success":true,"client_name":"Client 001"}]},"client_name":"Client 001"}]}]},"code":200}

这是我的UITableView代码

func numberOfSections(in tableView: UITableView) -> Int {
    if self.searchActive {
        return self.arrSearchedInvoiceList.count
    } else {
        return self.arrInvoiceList.count
    }
}
    
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
    if self.searchActive {
        let objDateInvoice = self.arrSearchedInvoiceList[section]
        let arrRows = objDateInvoice.arrInvoices
        return arrRows.count
    } else {
        let objDateInvoice = self.arrInvoiceList[section]
        let arrRows = objDateInvoice.arrInvoices
        return arrRows.count
    }
}
    
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "BossInvoiceHistoryCell",for: indexPath) as! BossInvoiceHistoryCell
       
    let objDateInvoice: BossInvoiceDateObject!
    let objInvoice: BossInvoiceObject!
        
    if self.searchActive {
        objDateInvoice = self.arrSearchedInvoiceList[indexPath.section]
        objInvoice = objDateInvoice.arrInvoices[indexPath.row]
    } else {
        objDateInvoice = self.arrInvoiceList[indexPath.section]
        objInvoice = objDateInvoice.arrInvoices[indexPath.row]
    }
        
    cell.lblClientName.text = objInvoice.client_name
    cell.lblAmount.text = objInvoice.currency + "\(objInvoice.invoice_total_amount)"
        
    cell.selectionStyle = .none
    return cell
}

我知道要像下面的代码那样从单个数组进行过滤。

self.arrSearchedInvoice = (self.arrActiveEmployee.filter{$0.client_name.localizedLowercase.localizedCaseInsensitiveContains(searchBar.text ?? "")} as NSArray) as! [BossInvoiceObject]

要求:我想根据内部对象的值进行搜索

任何人都可以指导我实现这一目标。

谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。