gpt4 book ai didi

swift - 找不到接受提供的参数的 '__conversion' 的重载,将多个元素添加到数组

转载 作者:搜寻专家 更新时间:2023-10-30 23:00:37 26 4
gpt4 key购买 nike

我定义了以下变量:

var mapHorizontalConstraints:Array = Array<NSLayoutConstraint>()

稍后,当我尝试使用可视格式语言设置约束数组的值时,如下所示:

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

我收到“找不到接受提供的参数的‘__conversion’的重载”,有一个小箭头指向 "=" 在上面的赋值中登录。

我相信这是因为我们不能使用 "=" 添加到数组。如果我可以使用数组的 append() 函数添加多个元素,我会尝试这样做,但如您所知,append() 只接受一个元素。

所以想知道这是否是我的问题,如果是,我如何在 Swift 中向数组添加多个元素?

最佳答案

您不能将变量设置为类型 Array ,你需要显式写出类型,像这样:

var mapHorizontalConstraints:Array<NSLayoutConstraint> = ...

或者更简单地说,

var mapHorizontalConstraints: [NSLayoutConstraint] = ...

如评论中所述,NSLayoutConstraint.constraintsWithVisualFormat返回 [AnyObject]!

因此当你赋值的时候,你应该这样向下转型:

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

您可以使用 += 附加到它运营商。

关于swift - 找不到接受提供的参数的 '__conversion' 的重载,将多个元素添加到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24209188/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com