gpt4 book ai didi

ios - 如何快速过滤字典并将结果输出到 CollectionViewController

转载 作者:搜寻专家 更新时间:2023-10-31 22:59:36 26 4
gpt4 key购买 nike

我正在制作一个显示口袋妖怪及其类型的应用程序。该应用程序的一部分也会显示他们的弱点。

我有一个列出所有口袋妖怪的全局变量,如下所示:

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 选择一个 Pokemon 时,他们然后按下一个按钮,它将那个 Pokemon 的对象传递到我的 CollectionViewController - 并将其存储在 var selectedPokemonObject = [String :String]()

我将 CollectionViewController 设置为过滤我的 Pokemon 对象并仅显示与用户选择的 Pokemon 类型相匹配的 Pokemon。例如:

用户选择:皮卡丘(类型“Electric”)

然后用户转到 CollectionViewController 并显示所有 "Electric" 类型的 Pokemon。我的一些代码是:

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

}

所有这些都有效。

我希望发生的事情

我不希望 CollectionViewController 显示与用户选择的相同类型的 Pokemon,而是显示具有我定义的 Type 的 Pokemon。例如:

用户选择:皮卡丘 ("type": "Electric")

然后用户转到CollectionViewController,所有"Ground" 类型的Pokemon 都会显示。或者,更复杂的情况:

用户选择:Bulbasaur(“type”:“Grass”“typeTwo”:“Poison”)

然后用户转到 CollectionViewController 和所有类型为 "Fire""Ice""Flying"的 Pokemon “Psychic” 显示。

我真的很难解决这个问题,所以非常感谢您的帮助。我查看了 Switch 语句但不确定这是否是正确的方法?谢谢

最佳答案

我的首选方法是最初设置一个字典,其中包含对每种类型都较弱的口袋妖怪。我会这样做,这样你就可以得到弱 Pokemon 的列表,而无需遍历整个 Pokemon 列表(如果你知道大 O 表示法,则 O(1) 时间而不是 O(N))。

在单独的 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)

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

所以现在,如果您选择一个 Pokemon,下面的代码会将弱弱的 Pokemon 的完整列表返回到 valueTypevalueTypeTwo

let valueType = "Fire"
let valueTypeTwo = "Electric"

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

关于ios - 如何快速过滤字典并将结果输出到 CollectionViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38798075/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com