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?
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.
try this
var mapHorizontalConstraints:AnyObject[] = []
to set use the '=' operator, to add to the array '+=' as in
mapHorizontalConstraints += [obj1, obj2, obj3]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With