Reputation: 289
I have an arrays
var searchArray = [(
ean: String,
name: String,
weight: String,
brand: String,
percent: String,
inside: String,
img: String,
packet: String,
date: String)
]()
var searchArrayFiltered = [(
ean: String,
name: String,
weight: String,
brand: String,
percent: String,
inside: String,
img: String,
packet: String,
date: String)
]()
I have a code for search from arrays and show result in table:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchArrayFiltered = searchText.isEmpty ? searchArray : searchArray.filter({(dataString: String) -> Bool in
return dataString.(of: searchText, options: .caseInsensitive) != nil
})
tableView.reloadData()
}
But in line return dataString.String(of: searchText, options: .caseInsensitive) != nil
i have an error:
Value of tuple type '(ean: String, name: String, weight: String, brand: String, percent: String, inside: String, img: String, packet: String, date: String)' has no member 'String'
If I change dataString.String to dataString.name, I have an error:
Cannot call value of non-function type 'String'
Please help me to do search from searchArray
for "name".
Upvotes: 0
Views: 86
Reputation: 115
create a struct for
struct Model {
var ean: String
var name: String
var weight: String
var brand: String
var percent: String
var inside: String
var img: String
var packet: String
var date: String
}
then apply filter on your [Model]
Upvotes: 0
Reputation: 285180
First of all you are discouraged from using a tuple as array type. Use a custom struct or class
Apple says:
Tuples are useful for temporary groups of related values. They’re not suited to the creation of complex data structures. If your data structure is likely to persist beyond a temporary scope, model it as a class or structure, rather than as a tuple.
There are two major issues:
dataString.range(of...
dataString
is not a string, it's a tuple (the type annotation is redundant)Change the function to
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchArrayFiltered = searchText.isEmpty ? searchArray : searchArray.filter({tuple -> Bool in
return tuple.name.range(of: searchText, options: .caseInsensitive) != nil
})
tableView.reloadData()
}
Upvotes: 3