44
class book{
    var nameOfBook: String!
}

var englishBooks=[book(),book(),book()]
var arr = englishBooks.filter {
    contains($0.nameOfBook, "rt")
}

I'm using this filter but with error cannot invoke filter with an argument

0

7 Answers 7

51

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
Sign up to request clarification or add additional context in comments.

Comments

17

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!)"}))

Comments

8

SWIFT 4.0

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

self.resultArray = self.upcomingAuctions.filter {
                    $0.auctionStatus == "waiting"
                }

Comments

6

2020 | SWIFT 5.1:

public filterStr = ""

public var books: [Book] = []

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

Comments

5

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) }

Comments

4

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

englishBooks.filter( { $0.nameOfBook.range(of: searchText, options: .caseInsensitive) != nil}

Comments

2

In Swift 4.2 use the remove(where:) functionality. filter isn't doing well with memory, remove(where:) does the job better.

To do what you want:

englishBooks.removeAll { !$0.nameOfBook.contains("English") }

1 Comment

case-sensitive(!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.