2016-09-13 weird error when creating constraint in Swift 3

Today, I've transitioned a project to Swift 3 and I bumped into a crash. The crash was as follows:

    -[_SwiftValue nsli_superitem]: unrecognized selector sent to instance 0x170249a50
    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue nsli_superitem]: unrecognized selector sent to instance 0x170249a50'
    *** First throw call stack:
    (0x1816741c0 0x1800ac55c 0x18167b278 0x181678278 0x18157259c 0x1820ab104 0x1820a9948 0x1820a879c 0x1820a8340 0x100176cc0 0x10012071c 0x100123474 0x1874b55c8 0x1874cd15c 0x1876556ac 0x18756dd74 0x18756d9dc 0x18756d940 0x1874b2738 0x18497b40c 0x1849700e8 0x18496ffa8 0x1848ecc64 0x1849140d0 0x1874a7e08 0x1816217dc 0x18161f40c 0x18161f89c 0x18154e048 0x182fd1198 0x187520818 0x18751b550 0x10002387c 0x1805305b8)
    libc++abi.dylib: terminating with uncaught exception of type NSException

Turns out this happens at the end of the following bit of code:

 scrollView = UIScrollView()
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(scrollView)
    contentView = UIView()
    contentView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.addSubview(contentView)
    let viewDict = [
        "contentView": contentView,
        "scrollView": scrollView,
    ]
    let vFormat = "V:|[contentView]|"
    let constraints = NSLayoutConstraint.constraints(withVisualFormat: vFormat, options: [], metrics: nil, views: viewDict)

Yeah, I've been creating UIView objects by hand instead of using Interface Builder. Like a savage! After playing with the debugger, it turns out the compiler infers the type of the viewDict as a [String: UIView?] and do notice that UIView optional.

The problem is fixed by declaring the viewDict as follows:

  let viewDict: [String: UIView] = .....

Just leaving this here for DuckDuckGo, plus I've answered a similar question on StackOverflow.