如何过滤字典并将输出结果输出到Swift中的CollectionViewController中 [英] How to filter a dictionary and output result into CollectionViewController in swift

查看:185
本文介绍了如何过滤字典并将输出结果输出到Swift中的CollectionViewController中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个显示宠物小精灵及其类型的应用程序。另外,应用程序的一部分将显示其弱点。

I am making an app that shows Pokemon and their types. Also part of the app will show their weaknesses.

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

I have a global variable that lists all the Pokemon, which looks as follows:

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中选择一个口袋妖怪,然后按一个按钮,并将该口袋妖怪的对象传递给我的 CollectionViewController ,并将其存储在 var selectedPokemonObject = [String:String]( )

When the user selects a Pokemon from the 1st VC, they then press a button and it passes the object of that Pokemon to my CollectionViewController - and stores this in var selectedPokemonObject = [String:String]()

我有我的 CollectionViewController 设置,然后过滤我的Pokemon对象和显示只有口袋妖怪匹配用户选择的口袋妖怪的类型。例如:

I have my CollectionViewController set up to then filter my Pokemon objects and display only Pokemon that match the type of Pokemon that the user selected. For example:

用户选择:皮卡丘(键入

然后,用户转到 CollectionViewController ,所有类型为$ code>Electric的口袋妖怪被显示。我的一些代码是:

The user then goes to the CollectionViewController and all Pokemon of type "Electric" are shown. Some of my code for that is:

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 如下:

I then set the cell info in the cellForItemAtIndexPath as follows:

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。例如:

Instead of the CollectionViewController showing Pokemon of the same type as the one the user selected, I'd like it to show Pokemon that has a Type that I define. For example:

用户选择:皮卡丘类型:电

然后,用户转到 CollectionViewController 和所有类型为的宠物小精灵被显示。或者,一个更复杂的情况:

The user then goes to the CollectionViewController and all Pokemon of type "Ground" are shown. Or, a more complex case:

用户选择: Bulbasaur type:Grass code>和typeTwo:Poison

然后用户转到 CollectionViewController 和所有口袋妖怪类型FireIce飞行Psychic

The user then goes to the CollectionViewController and all Pokemon of type "Fire", "Ice", "Flying" and "Psychic" are shown.

我真的很难想出这一点,所以任何帮助都不胜感激。我看过Switch的声明,但不知道这是否是正确的方法?谢谢

I'm really having trouble to figure this out, so any help is appreciated. I have looked at Switch statements but not sure if this is the right approach? Thanks

推荐答案

我的首选方法是初始设置一个包含每种类型弱的口袋妖怪的字典。我会这样做,所以你可以得到弱口袋妖怪的列表,没有循环遍历整个名单的口袋妖怪(O(1)时间,而不是O(N),如果你知道大O符号)。

My preferred approach would be to initially setup a Dictionary containing the Pokemon that are weak to each type. I would do this so you can get the list of weak Pokemon without looping through the entire list of Pokemon (O(1) time instead of O(N) if you know big-O notation).

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

In a separate Swift command-line project,

//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)

此代码应该输出完整的Pokemon弱点列表。您可以将该输出直接复制到项目中作为新变量(可能存储在您的对象的存储位置)。

This code should output the entire list of Pokemon weaknesses. You can directly copy that output into your project as a new variable (probably stored where your objects are stored).

所以现在,如果你选择一个口袋妖怪,下面的代码将返回完整的口袋妖怪软件列表到 valueType valueTypeTwo

So now, if you select a Pokemon, this below code will return the full list of Pokemon weak to valueType and valueTypeTwo.

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

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

这篇关于如何过滤字典并将输出结果输出到Swift中的CollectionViewController中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆