0

I have two arrays of data that I would like to search through and don't know how to search both with a UISearchController

struct Fish {
   var name: String
   var price: Int
}
var fishArray: [Fish] = [   Fish(name: "Bass", price: 1),
                            Fish(name: "Pike", price: 2),
                            Fish(name: "Carp", price: 3),
                            Fish(name: "Bluegill", price: 4),
                            Fish(name: "Catfish", price: 5)]
struct Weapons {
   var name: String
   var price: Int
}
var weaponsArray: [Weapons] = [   Weapons(name: "Sword", price: 1),
                                  Weapons(name: "Knife", price: 2),
                                  Weapons(name: "Gun", price: 3),
                                  Weapons(name: "Poison", price: 4),
                                  Weapons(name: "Hammer", price: 5)]

The searching happens here. And it works great to search through just the Weapons but how can I add in the Fish and multiple other arrays later.

func filterContentForSearchText(_ searchText: String, scope: String = "All") {
    filteredData = weaponsArray.filter({( weapon : Weapons) -> Bool in
        return weapon.name.lowercased().contains(searchText.lowercased())
    })

    mainTableView.reloadData()
}

Hope this makes sense to someone! Thanks!

2
  • If the properties of both structs are similar, why don't you have a single struct then? Commented Mar 5, 2019 at 3:00
  • I actually had to ask myself the same question... But my example of both having name and price is not the only things they would have... They would each have several other properties unique to them. That being said, I can potentially still make one struct work for what I’m doing. Thanks! Commented Mar 5, 2019 at 5:12

1 Answer 1

1

If they all have name and price, you can do something like this, for the last part you can upcast the other array and combine them into one big array :

protocol Item {
    var name: String { get set }
    var price: Int { get set }
}

struct Fish: Item {
    var name: String
    var price: Int
}
var fishArray: [Fish] = [   Fish(name: "Bass", price: 1),
                            Fish(name: "Pike", price: 2),
                            Fish(name: "Carp", price: 3),
                            Fish(name: "Bluegill", price: 4),
                            Fish(name: "Catfish", price: 5)]
struct Weapons: Item {
    var name: String
    var price: Int
}
var weaponsArray: [Weapons] = [   Weapons(name: "Sword", price: 1),
                                  Weapons(name: "Knife", price: 2),
                                  Weapons(name: "Gun", price: 3),
                                  Weapons(name: "Poison", price: 4),
                                  Weapons(name: "Hammer", price: 5)]

var items: [Item] = (fishArray as [Item]) + (weaponsArray as [Item])
items.filter { (item) -> Bool in
    return item.name.lowercased().contains("C".lowercased())
}
Sign up to request clarification or add additional context in comments.

1 Comment

This looks promising! I’ve never used protocols but seems like this may be a good option! Will see if this works! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.