I have the following code to try and create an array of constraints to add to a view:
let views = ["button": button]
let metrics = ["margin": 16]
var constraints: [AnyObject] = []
constraints += NSLayoutConstraint.constraintsWithVisualFormat("|-margin-[button]-margin-|", options: 0, metrics: metrics, views: views)
From what I understand about Swift arrays, I should just be able to '+=' them together to join the two, but I get an error:
"Binary operator '+=' cannot be applied to two [AnyObject] operands"
What's wrong with this code?
It's not because of the operator. It's because you are passing in an Int where you are actually supposed to pass
NSLayoutFormatOptions
enum type.If you pass in one of the
NSLayoutFormatOptions
enum for theoptions
parameter, the error will go away:Or you could also initialize the
NSLayoutFormatOptions
with the Int value that you want to use, like this:0 would have worked in Objective-C, but you need to use the actual enum value in Swift. The Swift errors are still often misleading in many cases, like this one.
Hope this helps.