Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not find an overload for '__conversion' that accepts the supplied arguments, add multiple elements to array

Tags:

swift

I've got following variable defined:

var mapHorizontalConstraints:Array = Array<NSLayoutConstraint>()

later when I'm trying to set the value of my constraints array using Visual Format Language as follow:

    mapHorizontalConstraints =  NSLayoutConstraint.constraintsWithVisualFormat("|-0-[mapView]-0-|",
        options: NSLayoutFormatOptions(0),
        metrics: nil,
        views: ["mapView":mapView])

I'm getting "Could not find an overload for '__conversion' that accepts the supplied arguments", with a little arrow pointing to "=" sign in above assignment.

I believe it's because we cannot add to array using "=". If I could add multiple elements using array's append() function I would have tried that, but as you know append() just accepts one single element.

So just wondering if this is my issue, and if so, how can I add multiple elements to array in Swift?

like image 726
Amir Rezvani Avatar asked Jun 13 '14 15:06

Amir Rezvani


2 Answers

You cannot set a variable to type Array, you need to explicitly write out the type, like this:

var mapHorizontalConstraints:Array<NSLayoutConstraint> = ...

or more simply,

var mapHorizontalConstraints: [NSLayoutConstraint] = ...

As mentioned in the comments, NSLayoutConstraint.constraintsWithVisualFormat returns [AnyObject]!

Thus when you assign it, you should downcast like this:

 mapHorizontalConstraints =  NSLayoutConstraint.constraintsWithVisualFormat("|-0-[mapView]-0-|",
        options: 0,
        metrics: nil,
        views: ["mapView":mapView]) as [NSLayoutConstraint]

You can append to it with the += operator.

like image 97
Jack Avatar answered Sep 28 '22 04:09

Jack


try this

    var mapHorizontalConstraints:AnyObject[] = []

to set use the '=' operator, to add to the array '+=' as in

mapHorizontalConstraints += [obj1, obj2, obj3]
like image 30
n13 Avatar answered Sep 28 '22 04:09

n13