How do i use array.filter to filter a class object based on a property?

主宰稳场 提交于 2019-12-17 16:54:35

问题


I'm trying to filter an array of instances of a class. I'd like a new array filtered by one of the class properties. Can't quite get my head around the way Swift filters work for this use case.

enum Gender {
    case male,female
}

class Person {
    let name:String
    let gender:Gender
    init(name:String,gender:Gender) {
        self.name = name
        self.gender = gender
    }
}


let people = [Person.init(name: "James", gender: .male),
              Person.init(name: "John", gender: .male),
              Person.init(name: "Sally", gender: .female)
             ]

let males = people.filter( something )

回答1:


This should work...

let males = people.filter({ $0.gender == .male })

You may need to make your enum conform to equatable to do this comparison.

The $0 is an unnamed parameter, you could also do..

let males = people.filter({ person in return person.gender == .male })

EDIT: I've just tested this and it does work without making the enum conform to equatable. I think you only need to do that when the enum takes parameters.




回答2:


enum TransactionMode {
    case credit, debit
}

class Transaction {
    public private(set) var pnr: String
    public private(set) var transactionMode: TransactionMode
    public private(set) var pointDescription: String
    public private(set) var date: String
    public private(set) var points: UInt16

    init(pnr:String, transactionMode:TransactionMode, pointDescription:String, date:String, points:UInt16) {
        self.pnr = pnr
        self.transactionMode = transactionMode
        self.pointDescription = pointDescription
        self.date = date
        self.points = points
    }
}

lazy var  transactionLists:Array<Transaction> = {
        let transactions:Array<Transaction>  =
            [Transaction(pnr: "PQ673W", transactionMode:.debit, pointDescription: "Received SpiceCash", date: "5th Mar,2018", points: 700),
             Transaction(pnr: "PQ671W", transactionMode:.credit, pointDescription: "Redeemed SpiceCash", date: "5th Jun,2018", points: 400),
             Transaction(pnr: "MQ671X", transactionMode:.debit, pointDescription: "Redeemed Loyalty Points", date: "5th July,2017", points: 500),
             Transaction(pnr: "PQ671L", transactionMode:.credit, pointDescription: "Received SpiceCash", date: "18th Mar,2018", points: 600),
             Transaction(pnr: "PQ671D", transactionMode:.debit, pointDescription: "Redeemed SpiceCash", date: "15th Jun,2018", points: 400),
             Transaction(pnr: "MQ671Q", transactionMode:.credit, pointDescription: "Redeemed Loyalty Points", date: "25th April,2017", points: 500),
             Transaction(pnr: "P2671L", transactionMode:.debit, pointDescription: "Received SpiceCash", date: "18th Jan,2018", points: 1200),
             Transaction(pnr: "PQ671Q", transactionMode:.credit, pointDescription: "Redeemed SpiceCash", date: "15th Feb,2018", points: 1400),
             Transaction(pnr: "MQ677A", transactionMode:.debit, pointDescription: "Redeemed Loyalty Points", date: "25th April,2017", points: 1500)
        ]
        return transactions
    }()

let filteredArray = self.transactionLists.filter({
                ($0.pnr.localizedCaseInsensitiveContains(searchText)) || (String(format: "%d", ($0.points)).localizedCaseInsensitiveContains(searchText)) || ($0.pointDescription.localizedCaseInsensitiveContains(searchText)) || ($0.date.localizedCaseInsensitiveContains(searchText))
            })

let pnr = self.transactionLists.filter({ $0.pnr == "PQ671Q"})


来源:https://stackoverflow.com/questions/42647994/how-do-i-use-array-filter-to-filter-a-class-object-based-on-a-property

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!