title: Programmatically adding multiple subviews with constraints throws exception
body: <p><h1>Environment</h1><ul><li>Xcode-8</li><li>Swift-3</li><li>iOS-10</li></ul>
  <h1>Intent</h1><ul>The germain intent is to get three subviews added to the main view, such that they appear sequentially adjacent starting from the top of the screen.<br/>There's more to the ultimate intent, but this is where I'm currently stuck.</ul>
  <h1>Working Code</h1><ul>If I'm just adding a single subview, it works with the following code (<i>slightly abridged, line numbers added for reference</i>):</ul></p>
  
  <pre><code>01: class ViewController: UIViewController, UITextFieldDelegate {
  02:    var textMatte: UIView!
  
  03:    override func viewDidLoad() {
  04:        super.viewDidLoad()
  05:        setupTextfieldMatte(inView: view)
  06:    }
  
  07:    func setupTextfieldMatte(inView: UIView) {
  08:        textMatte = UIView()
  09:        textMatte.backgroundColor = UIColor.lightGray
  10:        textMatte.translatesAutoresizingMaskIntoConstraints = false
  11:        inView.addSubview(textMatte)
  12:        let tmGuide = textMatte.layoutMarginsGuide
  13:        let inGuide = inView.layoutMarginsGuide
  14:        tmGuide.topAnchor.constraint(equalTo: inGuide.topAnchor, constant: 40).isActive = true
  15:        tmGuide.leadingAnchor.constraint(equalTo: inGuide.leadingAnchor, constant: 20).isActive = true
  16:        tmGuide.trailingAnchor.constraint(equalTo: inGuide.trailingAnchor, constant: -20).isActive = true
  17:        tmGuide.heightAnchor.constraint(equalToConstant: 40).isActive = true
  18:    }
  19:    //... other code ...
  20: }
  </code></pre>
  
  <p><h1>Broken Code</h1><ul>I wanted to break-up the <i>creation</i> step from the <i>constraint</i> step, in order to do "batch" processing (and because there's more that needs to happen, as eventually <i>these</i> subviews will also have their own subviews...). Added relative line numbers for ease of further description and (hopefully) answers from you folks:</ul></p>
  
  <pre><code>01: class ViewController: UIViewController, UITextFieldDelegate {
  02:     var mattes = [UIView()]
  
  03:     override func viewDidLoad() {
  04:         super.viewDidLoad()
  05:         for (index, title) in ["A", "B", "C"].enumerated() { // genericized for posting
  06:            let v = createView(idx: index)
  07:             mattes.append(v)
  08:             view.addSubview(v)
  09:         }
  10:         _ = mattes.popLast() //from testing/debugging found that there was an extraneous entry in the array
  11:         addViewAnnotations(views: mattes, inView: self.view)
  12:     }
  
  13:     func createView(idx: Int) -&gt; UIView {
  14:         let v = UIView()
  15:         v.backgroundColor = UIColor.lightGray
  16:         v.translatesAutoresizingMaskIntoConstraints = false
  17:         v.tag = idx
  18:         return v
  19:     }
  
  20:     func addViewAnnotations(views: [UIView], inView: UIView) {
  21:         let inGuide = inView.layoutMarginsGuide
  22:         for (index, v) in views.enumerated() {
  23:             let myGuide = v.layoutMarginsGuide
  24:             if index == 0 {
  25:                 myGuide.topAnchor.constraint(equalTo: inGuide.topAnchor, constant: 40).isActive = true
  ***                 //^^^ libc++abi.dylib: terminating with uncaught exception of type NSException
  26:             }
  27:             else {
  28:                 myGuide.topAnchor.constraint(equalTo: views[index-1].layoutMarginsGuide.bottomAnchor, constant: 10).isActive = true
  29:             }
  30:             myGuide.leadingAnchor.constraint(equalTo: inGuide.leadingAnchor, constant: 20).isActive = true
  31:             myGuide.trailingAnchor.constraint(equalTo: inGuide.trailingAnchor, constant: -20).isActive = true
  32:             myGuide.heightAnchor.constraint(equalToConstant: 40).isActive = true
  33:         }
  34:     }
  35:     //... other code ...
  36: }
  </code></pre>
  
  <ul>The error occurs on line 25 (error message in comment below that line), verified with <code>print</code> statements above and below the line.<br/>There's no additional error/debugging information that I can see<br/>I had expected that it <i>might</i> break on line 28, but 25 (in the "broken code") is identical to line 14 (in the working code)<br/><b>Note:</b> If I put a <code>try</code> in from of the line, Xcode tells me: "No calls to throwing functions occur within 'try' expression"</ul>
  
  <p>I have a feeling that this is one of those stupid oversights that crop up so often when modifying code, but I just cannot seem to spot the source of the problem - so I'm hoping one of you can.</p>
  
  <p><h1>UPDATE 2016-10-16</h1><ul>Based on my modification as described in the first comment I added, I played around with the code a bit more and found that the problem occurs only when trying to make [what is now] the last constraint <i>active</i>:</ul></p>
  
  <pre><code>001:    override func viewDidLoad() {
  002:        super.viewDidLoad()
  
  003:        for (index, title) in ["Small Blind", "Big Blind", "Ante"].enumerated() {
  004:            let v = createView(idx: index)
  005:            mattes.append(v)
  006:            view.addSubview(v)
  007:            let myGuide = v.layoutMarginsGuide
  008:            let inGuide = view.layoutMarginsGuide
  009:            myGuide.leadingAnchor.constraint(equalTo: inGuide.leadingAnchor, constant: 20 ).isActive = true
  010:            myGuide.trailingAnchor.constraint(equalTo: inGuide.trailingAnchor, constant: -20).isActive = true
  011:            myGuide.heightAnchor.constraint(equalToConstant: 40).isActive = true
  012:            if index == 0 {
  013:                myGuide.topAnchor.constraint(equalTo: inGuide.topAnchor, constant: 40).isActive = true
  014:            }
  015:            else {
  016:                let x = myGuide.topAnchor.constraint(equalTo: mattes[index-1].bottomAnchor, constant: 10)
  017:                x.isActive = true
                      //^^^ libc++abi.dylib: terminating with uncaught exception of type NSException
  018:            }
  019:        }
  020:    }
  </code></pre>
  
  <ul>All the other constraints have no issues. It doesn't seem to matter if I use:<ul><li><code>mattes[index-1].bottomAnchor</code></li><li><code>mattes[index-1].layoutMarginsGuide.bottomAnchor</code></li></ul>The first one evaluates to <code>&lt;NSLayoutYAxisAnchor:0x174076540 "UIView:0x100b0d380.bottom"></code>, the second to <code>&lt;NSLayoutYAxisAnchor:0x174076c00 "UILayoutGuide:0x174191e00'UIViewLayoutMarginsGuide'.bottom"></code> (<i>if that matters?</i>)<br/>If I comment out line 017, the code runs, but while the first rectangle is in the correct location, the remaining two are not - they're flush against the top of the screen instead of below the top indicators.<br/>So - perhaps there's a better way to set them up?</ul>