Swift filter array of objects

前端 未结 7 939
忘了有多久
忘了有多久 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:04

    Sorry this is an old thread. Change you code slightly to properly init your variable 'nameOfBook'.

    class book{
       var nameOfBook: String!
       init(name: String) {
          nameOfBook = name
       }
    }
    

    Then we can create an array of books.

    var englishBooks = [book(name: "Big Nose"), book(name: "English Future 
    Prime Minister"), book(name: "Phenomenon")]
    

    The array's 'filter' function takes one argument and some logics, 'contains' function can take a simplest form of a string you are searching for.

    let list1 = englishBooks.filter { (name) -> Bool in
       name.contains("English")
    }
    

    You can then print out list1 like so:

    let list2 = arr1.map({ (book) -> String in
       return book.nameOfBook
    })
    print(list2)
    
    // print ["English Future Prime Minister"]
    

    Above two snippets can be written short hand like so:

    let list3 = englishBooks.filter{ ($0.nameOfBook.contains("English")) }
    print(list3.map({"\($0.nameOfBook!)"}))
    
    0 讨论(0)
  • 2021-02-03 21:08

    I think this is more useful for lack of wrong typing situation.

    englishBooks.filter( { $0.nameOfBook.range(of: searchText, options: .caseInsensitive) != nil}
    
    0 讨论(0)
  • 2021-02-03 21:09

    SWIFT 4.0

    In order to filter objects and get resultant array you can use this

    self.resultArray = self.upcomingAuctions.filter {
                        $0.auctionStatus == "waiting"
                    }
    
    0 讨论(0)
  • 2021-02-03 21:12

    in case you want to delete an interval of object which has specific IDs (matchIDsToDelete) from an array of object (matches)

    var matches = [Match]
    var matchIDsToDelete = [String]
    matches = matches.filter { !matchIDsToDelete.contains($0.matchID) }
    
    0 讨论(0)
  • 2021-02-03 21:14

    contains() checks if a sequence contains a given element, e.g. if a String contains a given Character.

    If your intention is to find all books where the name contains the substring "rt", then you can use rangeOfString():

    var arr = englishBooks.filter {
        $0.nameOfBook.rangeOfString("rt") != nil
    }
    

    or for case-insensitive comparison:

    var arr = englishBooks.filter {
        $0.nameOfBook.rangeOfString("rt", options: .CaseInsensitiveSearch) != nil
    }
    

    As of Swift 2, you can use

    nameOfBook.containsString("rt") // or
    nameOfBook.localizedCaseInsensitiveContainsString("rt")
    

    and in Swift 3 this is

    nameOfBook.contains("rt") // or
    nameOfBook.localizedStandardContains("rt") // or
    nameOfBook.range(of: "rt", options: .caseInsensitive) != nil
    
    0 讨论(0)
  • 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 }
       }
    }
    
    
    0 讨论(0)
提交回复
热议问题