'+=' cannot be applied to two [AnyObject] operands

762 Views Asked by At

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?

4

There are 4 best solutions below

2
On BEST ANSWER

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 the options parameter, the error will go away:

constraints += NSLayoutConstraint.constraintsWithVisualFormat("|-margin-[button]-margin-|", options: .AlignAllLeft, metrics: metrics, views: views)

Or you could also initialize the NSLayoutFormatOptions with the Int value that you want to use, like this:

NSLayoutFormatOptions(rawValue: 0)

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.

1
On

I think you could try saving the results of constraintsWithVisualFormat into a temporary constant, then assigning it.

let newConstraints = NSLayoutConstraint.constraintsWithVisualFormat("|-margin-[button]-margin-|", options: 0, metrics: metrics, views: views)
constraints += newConstraints
0
On

Here's the answer:

Xcode was listing the wrong thing as the error, which is what led to all this confusion. Using an Int of 0 for my format options apparently does not make the compiler happy, so instead I had to use NSLayoutFormatOptions(0).

With that, the finished working code is like this:

var constraints: [AnyObject] = []
constraints += NSLayoutConstraint.constraintsWithVisualFormat("|-margin-[button]-margin-|", options: NSLayoutFormatOptions(0), metrics: metrics, views: views)
0
On

In my case using .AlignAllLeft instead of NSLayoutFormatOptions(rawValue: 0) was not what caused the error. The array of constraints should have been statically-typed.

// BEFORE:
// var viewConstraints: [AnyObject] = [AnyObject]()
// AFTER:  
var viewConstraints: [NSLayoutConstraint] = [NSLayoutConstraint]()
let views = NSDictionary(object: customView, forKey: "customView")

viewConstraints += NSLayoutConstraint.constraintsWithVisualFormat("H:|[customView]|",
    options: NSLayoutFormatOptions(rawValue: 0),
    metrics: nil,
    views: views as! [String : AnyObject])

// "Convert to Swift 2.0" Xcode option suggests syntax:
viewConstraints += NSLayoutConstraint.constraintsWithVisualFormat("V:|[customView]|",
    options: [],
    metrics: nil,
    views: views as! [String : AnyObject])

That appears good, because I want "no options" value.