如何筛选字典和输出结果到Swift的CollectionViewController

我正在制作一个显示口袋妖怪及其types的应用程序。 也是应用程序的一部分将显示其弱点。

我有一个全局variables,列出所有的口袋妖怪,看起来如下:

var objects = [ { "id": "001", "typeTwo": "Poison", "name": "Bulbasaur", "type": "Grass" }, { "id": "002", "typeTwo": "Poison", "name": "Ivysaur", "type": "Grass" }, { "id": "025", "typeTwo": "", "name": "Pikachu", "type": "Electric" }] 

等等…

当用户从第一个VC中select一个口袋妖怪时,他们按下一个button,它将该口袋妖怪的对象传递给我的CollectionViewController – 并将其存储在var selectedPokemonObject = [String:String]()

我有我的CollectionViewController设置,然后筛选我的宠物小精灵对象,并显示只匹配口袋妖怪,用户selecttypes的口袋妖怪。 例如:

用户select: 皮卡丘"Electric"

用户然后去CollectionViewController和types"Electric"所有口袋妖怪被显示。 我的一些代码是:

 var filteredObjects = [[String:String]]() override func viewDidLoad() { super.viewDidLoad() filteredObjects = objects.filter{ let valueType = $0["type"] let valueTypeTwo = $0["typeTwo"] if (valueType == selectedPokemonObject["type"] || valueType == selectedPokemonObject["typeTwo"]) && (valueTypeTwo == selectedPokemonObject["typeTwo"] || valueTypeTwo == selectedPokemonObject["type"]) { return true } return false } } 

然后我设置cellForItemAtIndexPath的单元格信息,如下所示:

 override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell let object = filteredObjects[indexPath.row] cell.pokemonName.text = object["name"]! cell.pokemonImage.image = UIImage(named: "\(object["id"]!)"+"\(object["name"]!)"+"_small") return cell } 

所有这些工作。

我想要发生什么

而不是显示与用户select相同types的口袋妖怪的CollectionViewController ,我希望它显示具有我定义的types的口袋妖怪。 例如:

用户select: 皮卡丘"type": "Electric"

用户然后去CollectionViewController和types"Ground"所有口袋妖怪被显示。 或者,更复杂的情况是:

用户select: Bulbasaur"type": "Grass""typeTwo": "Poison"

然后用户进入CollectionViewController并显示"Fire""Ice""Flying""Psychic"types的所有口袋妖怪。

我真的很难解决这个问题,所以任何帮助表示赞赏。 我已经看过Switch的声明,但不知道这是否是正确的方法? 谢谢

我最喜欢的方法是最初设置一个包含每个types弱的口袋妖怪的字典。 我会这样做,所以你可以得到弱口袋妖怪列表没有循环通过口袋妖怪的整个列表(O(1)时间而不是O(N),如果你知道大O符号)。

在一个单独的Swift命令行项目中,

 //Assuming objects is setup similar to below var objects = [ ["id": "001", "typeTwo": "Poison", "name": "Bulbasaur", "type": "Grass"], ["id": "002", "typeTwo": "Poison", "name": "Ivysaur", "type": "Grass"], ["id": "025", "typeTwo": "", "name": "Pikachu", "type": "Electric"]] var pokemonTypeDefenseChart = [String: [String]]() pokemonTypeDefenseChart["Grass"] = ["Fire", "Ice", "Flying"] pokemonTypeDefenseChart["Poison"] = ["Psychic", "Ground"] pokemonTypeDefenseChart["Electric"] = ["Ground"] // Setup rest of the weaknesses (this part is manual unfortunately) var pokemonWeaknessChart = [String: [String]]() func getWeaknesses(type: String, name: String) { for weakness in pokemonTypeDefenseChart[type]! { if pokemonWeaknessChart[weakness] == nil { pokemonWeaknessChart[weakness] = [] } pokemonWeaknessChart[weakness]?.append(name) } } for object in objects { if let type = object["type"] where !type.isEmpty, let name = object["name"] { getWeaknesses(type, name: name) } if let typeTwo = object["typeTwo"] where !typeTwo.isEmpty, let name = object["name"] { getWeaknesses(typeTwo, name: name) } } print(pokemonWeaknessChart) 

这个代码应该输出口袋妖怪弱点的整个列表。 您可以直接将该输出复制到您的项目中作为一个新的variables(可能存储在您的objects存储的地方)。

所以现在,如果你select一个宠物小精灵,这下面的代码将返回完整的宠物小精灵弱列表valueTypevalueTypeTwo

 let valueType = "Fire" let valueTypeTwo = "Electric" var pokemonWeaknesses = pokemonWeaknessChart[valueType] if !valueTypeTwo.isEmpty, let pokemonTypeTwoWeaknesses = pokemonWeaknessChart[valueTypeTwo] { pokemonWeaknesses?.appendContentsOf(pokemonTypeTwoWeaknesses) } 
Interesting Posts