Swift filter array of objects

前端 未结 7 942
忘了有多久
忘了有多久 2021-02-03 20:44
class book{
    var nameOfBook: String!
}

var englishBooks=[book(),book(),book()]
var arr = englishBooks.filter {
    contains($0.nameOfBook, \"rt\")
}
<
7条回答
  •  悲&欢浪女
    2021-02-03 21:16

    2020 | SWIFT 5.1:

    short answer:

    books.filter { $0.alias.range(of: filterStr, options: .caseInsensitive) != nil }

    long sample:

    public filterStr = ""
    
    public var books: [Book] = []
    public var booksFiltered: [Book] {
       get {
          (filterStr.isEmpty )
             ? books
             : books.filter { $0.alias.range(of: filterStr, options: .caseInsensitive) != nil }
       }
    }
    
    

提交回复
热议问题