title: Adding the values of two [String : Int] dictionaries in Swift
body: <p>So i am trying to add the values of two <code>[String : Int]</code> dictionaries in Xcode using the latest Swift. My code is shown below. Both dictionaries have keys from <code>q1</code> to <code>q23</code> with values attached. This is why the random number stops at 23. The key <code>q1</code> has no use in this function (which is why the random number starts at 2). The  dictionary values are given by by another view controller during the segue to this viewController, I do not assign them manually. There is no need for the values in the dictionaries to be optionals as they <strong>will</strong> have been assigned values to get to this viewController.</p>
  
  <p>I've been googling for an hour, but I cannot understand the reduce, combine or map functions as they would apply to this, and doing something so complex when I simply want to add seems like I'm overlooking an easier method.</p>
  
  <pre><code>var p1Pre:[String:Int]  = [“q1” : 1]
  
  var p2Pre:[String:Int]  = [“q1” : 1]
  var combPre:[String:Int]  = [“q1” : 0]
  
  var pick = 0
  
  // generates random number 2-23
  func pickRandom(){
      pick = Int.random(in:2...23)
  } 
  
  func addPrefs(){
      // this is where i'm having trouble
      combPre[“q\(pick)] = p1Pre[“q\(pick)”] + p2Pre[“q\(pick)”]
      print(combPre[“q\(pick)”]
      // I get an error at the plus sign here
  }
  </code></pre>
  
  <p>I'm trying to add the values at specific keys inside a two dictionaries (<code>p1Pre</code> and <code>p2Pre</code>) and the result will be stored in combPre.   I want to build a function so if <code>p1Pre[“q2”] == 1</code> and  <code>p2Pre[“q2”] == 2</code>, then <code>combPre[“q2”] = 3</code>.
  I get an error at the plus sign that says <code>“binary operator “+” cannot be applied to two ‘Int?’ Operands"</code>. How would I make this work? Thank you</p>