将1 Dictionary中的项添加到另一个Dictionary -Swift iOS9

时间:2016-09-08 20:44:59

标签: ios arrays swift loops dictionary

  1. 我有一个名为buttonPressed的按钮。
  2. 我有一个带有苏打水的sodaArray属性。
  3. 我有一个空的foodDict属性,后来我用键/值对填充。
  4. 我有一个空的sodaMachineArray属性我必须把苏打水放入。我使用一个函数将sodas附加到它中,然后使用另一个函数为它们分配键/值对以添加到foodDict中。我把它全部放在名为addSodas()的1函数中。
  5. 在buttonPressed操作中,我运行addSodas()函数。第二,我用不同的值填充foodDict。我需要将两个词典附加在一起,以便foodDict中包含所有苏打水,并附上当前的值。

    我遇到的问题是必须首先使用addSodas()函数(我别无选择)。既然那是第一个,而foodDict是第二个,我如何组合两个词典?

    class ViewController: UIViewController {
    
    //soda values already in the sodaArray
    var sodaArray = ["Coke", "Pepsi", "Gingerale"]
    
    //I add the soda values to this empty array(I have no choice)
    var sodaMachineArray = [String]()
    
    //This is a food dictionary I want to add the sodas in
    var foodDict = [String: AnyObject]()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    
    //Function to add sodas to the foodDict
    func addSodas(){
    
        //Here I append the sodas into the sodaMachineArray
        for soda in self.sodaArray {
            self.sodaMachineArray.append(soda)
        }
    
        //I take the sodaMachineArray, grab each index, cast it as a String, and use that as the Key for the key/value pair
        for (index, value) in self.sodaMachineArray.enumerate(){
            self.foodDict[String(index)] = value
        }
        print("\nA. \(self.foodDict)\n")
    }
    
    
    //Button
    @IBAction func buttonPressed(sender: UIButton) {
    
        self.addSodas()
        print("\nB. \(self.foodDict)\n")
    
        self.foodDict = ["KFC": "Chicken", "PizzaHut": "Pizza", "McDonalds":"Burger"]
        print("\nD. food and soda key/values should print here: \(self.foodDict)???\n")
    
        /*I need the final outcome to look like this  
    self.foodDict = ["0": Coke, "McDonalds": Burger, "1": Pepsi, "KFC": Chicken, "2": Gingerale, "PizzaHut": Pizza]*/
            }
        }
    

    顺便说一下,我知道我可以使用下面的方法扩展Dictionary,但在这种情况下它没用,因为addSodas()函数必须在foodDict填满之前到来。此扩展程序有效,但我无法将其用于我的场景。

    extension Dictionary {
        mutating func appendThisDictWithKeyValuePairsFromAnotherDict(anotherDict:Dictionary) {
            for (key,value) in anotherDict {
                self.updateValue(value, forKey:key)
            }
        }
    }
    

1 个答案:

答案 0 :(得分:6)

问题:

self.foodDict = ["KFC": "Chicken", "PizzaHut": "Pizza", "McDonalds":"Burger"]

此行创建新数组并分配这些值。

解决方案:

  1. 拥有持有关键值的临时财产[“KFC”:“Chicken”,“PizzaHut”:“Pizza”,“McDonalds”:“Burger”]。
  2. 迭代并将其分配给FoodDict。
  3. 实施例

    //Button
    @IBAction func buttonPressed(sender: UIButton) {
    
        self.addSodas()
        print("\nB. \(self.foodDict)\n")
    
        var tempFoodDict = ["KFC": "Chicken", "PizzaHut": "Pizza", "McDonalds":"Burger"]
        for (key, value) in tempFoodDict {
            self.foodDict[String(key)] = String(value)
        }
        print("\nD. food and soda key/values should print here: \(self.foodDict)???\n")
    }