Search property from class in array within UITableView

Pieter

Context: Swift, iOS8-9, UITableView, UISearchController. Code based on this example: http://www.ioscreator.com/tutorials/add-search-table-view-tutorial-ios8-swift

func updateSearchResultsForSearchController(searchController: UISearchController) {
    filteredTableData.removeAll(keepCapacity: false)
    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
    let scores = (allScores as NSArray).filteredArrayUsingPredicate(searchPredicate)
    filteredTableData = scores as! [Score]
    tableView.reloadData()
}

Problem: my data are from a class Score which has a property name and that is what I need to look in (ideally name and topic, but with name I'd be happy for now). So allScores as NSArray is not working because Score does not contain text, but the name property does. How can I implement that here? Something like allScores.name is obviously not working, but I don't know how to call the name property of all items in an array in general.

hennes

If you want to use NSPredicate, derive your Score class from NSObject and simply use the property name in the predicate (no SELF required).

class Score: NSObject {
    var name: String

    init(name: String) {
        self.name = name
    }

    override var description: String {
        return self.name
    }
}

let allScores = [Score(name: "a"), Score(name: "ab"), Score(name: "b")]
let searchPredicate = NSPredicate(format: "name CONTAINS[c] %@", "b")
let scores = (allScores as NSArray).filteredArrayUsingPredicate(searchPredicate)
print(scores)

The above snippet prints [ab, b] on the console.

However, you can achieve the very same result by using Swift's filter method.

let allScores = [Score(name: "a"), Score(name: "ab"), Score(name: "b")]
let scores = allScores.filter { $0.name.containsString("b") }
print(scores)

This snippet again prints [ab, b] on the console which demonstrates its equality to the NSPredicate variant.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Search property from class in array within UITableView

From Dev

access python property from within class

From Dev

Objective-c How To Segue from within a UITableView Sub Class

From Dev

UITableViewCell super class property returning nil when set from UITableView

From Dev

How to search cell in UITableView from multiple array in IOS

From Dev

Search for keys from an array within values of another array and replace

From Dev

How to access prototype property from within javascript class?

From Dev

How to call `property_exists()` on a class property from within said class?

From Dev

Accessing object from array within an array (defined in model class)

From Dev

Property Changed event within class

From Dev

Array of closures within a class

From Dev

Resizing an array within a class

From Dev

Getting a property of an object within an array

From Dev

Search Within Array Object in Swift

From Dev

Search for an object within an Array with a loop

From Dev

Assign a value to a property of a class within a class

From Dev

how to assign value class within class property

From Dev

how to assign value class within class property

From Dev

Instantiate class from within a class

From Dev

Array Property in VBA Class

From Dev

Returning an array from configuration file to class script as a property

From Dev

Javascript array property in a class only returned if its called from a function

From Dev

How to Search Array of Dictionary and show in UITableview?

From Dev

clicking button within UITableview cell gives wrong result while search

From Dev

Search array within another array - Algorithm

From Dev

Using .filter to search through an array within an array

From Dev

Using .filter to search through an array within an array

From Dev

PHP search letter in array within array

From Dev

Search in array with property name in JQuery

Related Related

  1. 1

    Search property from class in array within UITableView

  2. 2

    access python property from within class

  3. 3

    Objective-c How To Segue from within a UITableView Sub Class

  4. 4

    UITableViewCell super class property returning nil when set from UITableView

  5. 5

    How to search cell in UITableView from multiple array in IOS

  6. 6

    Search for keys from an array within values of another array and replace

  7. 7

    How to access prototype property from within javascript class?

  8. 8

    How to call `property_exists()` on a class property from within said class?

  9. 9

    Accessing object from array within an array (defined in model class)

  10. 10

    Property Changed event within class

  11. 11

    Array of closures within a class

  12. 12

    Resizing an array within a class

  13. 13

    Getting a property of an object within an array

  14. 14

    Search Within Array Object in Swift

  15. 15

    Search for an object within an Array with a loop

  16. 16

    Assign a value to a property of a class within a class

  17. 17

    how to assign value class within class property

  18. 18

    how to assign value class within class property

  19. 19

    Instantiate class from within a class

  20. 20

    Array Property in VBA Class

  21. 21

    Returning an array from configuration file to class script as a property

  22. 22

    Javascript array property in a class only returned if its called from a function

  23. 23

    How to Search Array of Dictionary and show in UITableview?

  24. 24

    clicking button within UITableview cell gives wrong result while search

  25. 25

    Search array within another array - Algorithm

  26. 26

    Using .filter to search through an array within an array

  27. 27

    Using .filter to search through an array within an array

  28. 28

    PHP search letter in array within array

  29. 29

    Search in array with property name in JQuery

HotTag

Archive