title: Swift 3 - How to remove all Subviews from Superview as well as Array
body: <p>I currently have a double for-loop that creates an X by Y grid of UIView CGRect squares. The loop also adds each UIView/Square of the grid to a 2D array allowing me to access each Cell of the grid and alter color/positioning by the index values of the loop.</p>
  
  <p>The loop seems to work fine and displays the Cells/Squares perfectly, however after a while I want to remove all of the squares and also empty the array (but not entirely delete) to make room for a new next grid (which may be of a different size). I created a function to remove all the views from the superview.</p>
  
  <p>This is how I am creating each "Cell" of the grid and placing each into the 2D array:</p>
  
  <pre><code>let xStart = Int(size.width/2/2) - ((gridSizeX * gridScale)/2)
  let yStart = Int(size.height/2/2) - ((gridSizeY * gridScale)/2)
  
  let cell : UIView!
  cell = UIView(frame: CGRect(x: xStart + (xPos * gridScale), y:yStart + (yPos * gridScale), width:gridScale, height:gridScale))
  
  cell.layer.borderWidth = 1
  cell.layer.borderColor = UIColor(red:0.00, green:0.00, blue:0.00, alpha:0.02).cgColor
  cell.backgroundColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:0)
  
  cell.tag = 100
  
  self.view?.addSubview(cell)
  
  gridArray[xPos][yPos] = cell
  </code></pre>
  
  <p>The 2D array is being created on application load like so:</p>
  
  <pre><code>gridArray = Array(repeating: Array(repeating: nil, count: gridSizeY), count: gridSizeX)
  </code></pre>
  
  <p>I have tried to search for a solution to remove the UIViews from the superview however all answers from other Questions don't seem to work for me. I have tried to add <code>cell.tag = 100</code> to each UIView and then remove all UIViews with the Tag Value of 100 like:</p>
  
  <pre><code>for subview in (self.view?.subviews)! {
      if (subview.tag == 100) {
          subview.removeFromSuperview()
      }
  }
  </code></pre>
  
  <p>However no luck. I have also tried to use this method: </p>
  
  <pre><code>self.view?.subviews.forEach({
      $0.removeConstraints($0.constraints)
      $0.removeFromSuperview()
  })
  </code></pre>
  
  <p>The main differences I notice in my code compared to other peoples answers is that I have "?" and "!" at places in the code. I researched about what they mean and understood most of it however I still do not know how to fix my code because if it, and feel as though that is the issue. All I know is that the attempts to remove the UIViews from the superview does not work, and without the "?" and "!"s the code doesn't run at all.</p>