Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert value of type '[NSLayoutConstraint]' error in swift 2

I want to add constraint to the UIImageView by adding this line of code:

addConstraint(NSLayoutConstraint.constraintsWithVisualFormat("H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": userProfileImageView]))

But xcode show me this error:

enter image description here

How I can fix this error?

like image 816
Sajad Avatar asked Aug 09 '16 09:08

Sajad


2 Answers

Use addConstraints, instead of addConstraint. constraintsWithVisualFormat returns an array.

Your code becomes:

addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[v0]|", options: [], metrics: nil, views: ["v0": userProfileImageView])
like image 113
James P Avatar answered Oct 21 '22 16:10

James P


let horizontalprofileViewConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": userProfileImageView]

If you click option and hover over horizontalprofileViewConstraint you will see its type as [NSLayoutConstraint] which is already an array.

So what you can do is:

view.addConstraints(horizontalprofileViewConstraint)

if you have more than one view then you cand do:

view.addConstraints(horizontalprofileViewConstraint + verticalprofileViewConstraint)

The + joins to arrays for you.

like image 44
mfaani Avatar answered Oct 21 '22 15:10

mfaani