I have a class like
class FoundItem : NSObject { var id : String! var itemName : String! var itemId : Int! var foundBy : String! var timeFound : String! init(id: String, itemName: String, itemId: Int, foundBy: String, timeFound: String) { self.id = id self.itemName = itemName self.itemId = itemId self.foundBy = foundBy self.timeFound = timeFound }
and I refer to him at
class MapViewVC: UIViewController, MKMapViewDelegate { var found = [FoundItem]() var filterItemName : String() }
My FoundItem generated into an array of dictionaries from my FoundItem class from a firebase request. Then I get the line of this itemName , which is created from another view controller, which is a collection view in the didSelection function. I want to take this string and then filter or search for arrays with the string itemName , which is equal to the string itemName from my previous viewController . Then delete the array of dictionaries that are not equal to itemName . Not only objects, but the entire array containing a pair of unequal keys, values. I searched for a few days and am stuck in filtering an array of dictionaries created from a class. I looked and tried NSPredicates, for-in loop, but all that ends is creating a new array or bool that finds my values ββor keys equal. Here is the current function I wrote.
func filterArrayBySearch() { if self.filterItemName != nil { dump(found) let namePredicate = NSPredicate(format: "itemName like %@", "\(filterItemName)") let nameFilter = found.filter { namePredicate.evaluate(with: $0) } var crossRefNames = [String: [FoundItem]]() for nameItemArr in found { let listName = nameItem.itemName let key = listName if crossRefNames.index(forKey: key!) != nil { crossRefNames[key!]?.append(nameItemArr) if !("\(key)" == "\(filterItemName!)") { print("------------- Success have found [[[[[[ \(key!) ]]]]]] and \(filterItemName!) to be equal!!") // crossRefNames[key!]?.append(nameItemArr) } else { print("!! Could not find if \(key!) and \(filterItemName!) are equal !!") } } else { crossRefNames[key!] = [nameItemArr] } } } else { print("No Data from Search/FilterVC Controller") } }
Can anyone help? It seems like it would be a simple task to find the value, and then filter out dictionaries that are not equal to the string itemName , but I continue to hit the wall. And I run into loops for myself: P is trying different things to achieve the same task.
source share