Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Invalid parameter not satisfying: constraint

I'm getting the error:

Invalid parameter not satisfying: [constraint isKindOfClass:[NSLayoutConstraint class]]

for my auto-layout constraints code written in Swift:

    let d:NSDictionary = ["scrollView": scrollView]
    let hc:NSArray = [NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollView]|", options: NSLayoutFormatOptions.allZeros, metrics: nil, views: d)]
    let vc:NSArray = [NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollView]|", options: NSLayoutFormatOptions.allZeros, metrics: nil, views: d)]
    view.addConstraints(hc)
    view.addConstraints(vc)

Does anyone have an idea what this error tries to tell me? As far as I know the parameter is of type NSArray with NSLayoutConstraint objects in it. I suspected I have to explicitly define the arrays as NSArray but that's not solving it.

like image 913
BadmintonCat Avatar asked Aug 26 '14 13:08

BadmintonCat


1 Answers

NSLayoutConstraint.constraintsWithVisualFormat already returns an NSArray so you don't need to wrap it in another array. So try:

let d:NSDictionary = ["scrollView": scrollView]
let hc:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollView]|", options: NSLayoutFormatOptions.allZeros, metrics: nil, views: d)
let vc:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollView]|", options: NSLayoutFormatOptions.allZeros, metrics: nil, views: d)
view.addConstraints(hc)
view.addConstraints(vc)
like image 199
Mike Pollard Avatar answered Oct 24 '22 02:10

Mike Pollard