Filter by array of Objects searchController

前端 未结 1 1199
梦如初夏
梦如初夏 2021-01-14 12:27

I have created an searchController and therefor i\'m trying t make it filter content according to the text in the UISearchController. I\'ve created a custom Object looking l

相关标签:
1条回答
  • 2021-01-14 12:45

    The filter() method of SequenceType does not take an NSPredicate as an argument, but a closure, e.g.

    let filteredTableData = sortedLocations.filter {
        $0.name.localizedCaseInsensitiveContainsString(searchText)
    }
    

    The closure is called for each array element (here using the shorthand argument $0) and returns true or false to indicate if the element is to be included in the filtered result or not.


    You can use an NSPredicate to filter an NSArray, that would look like

    let filtered = someNSArray.filteredArrayUsingPredicate(predicate)
    

    but there is no reason to use this if you have a Swift array.

    0 讨论(0)
提交回复
热议问题