Searching through objects in array swift

扶醉桌前 提交于 2019-12-03 13:50:52

The objects in the teamArray don't have a SELF property. You can't use SELF to search in all the properties of the object at once. You have to give the name of the property, and if you want to search in more than one you have to add all those properties to the predicate.

I would think it's enough for you to search in the name property like so:

let searchPredicate = NSPredicate(format: "name CONTAINS[c] %@", searchController.searchBar.text)

If you need in more properties you do like this:

let searchPredicate = NSPredicate(format: "name CONTAINS[c] %@ OR shortname CONTAINS[c] %@", searchController.searchBar.text, searchController.searchBar.text)

Can you post the definition of your Team object, and any sub-objects that it contains? (TeamData).

Also indicate where you expect the search text to appear in your team object.

I haven't used NSPRedicate a lot, but my understanding of the CONTAINS comparison is that it checks an individual field to see if it contains a substring. I don't think it will check all fields of the objects you're searching. That seems like what you're expecting.

Britto Thomas
let searchPredicate = NSPredicate(format: "name contains[c] %@", searchWord)
self.filteredArray = self.array.filteredArrayUsingPredicate(searchPredicate)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!