我越来越extraneous argument label 'contentsOf:' in call
array.append(contentsOf: test)想在操场上运行该代码时出错:
import Cocoa
var array:[Any] = []
let test = [""]
array.append(contentsOf: [""])
array.append(contentsOf: test)
Run Code Online (Sandbox Code Playgroud)
为什么会这样?据我所知,有两个相等的数组,空字符串.
protocol Test{
var t: Int {set get}
}
struct A: Test{
var t: Int
}
struct B: Test{
var t: Int
var s: String
}
let a = A(t: 1)
let b = B(t: 2, s: "1")
let c = a as Test
let d: [Test] = [a, b]
let e: [A] = [a, a]
let f = e as [Test]
let g = e as! [Test]
Run Code Online (Sandbox Code Playgroud)
"让f = e作为[测试],让g = e为![测试]"总是出错.我有一个功能,我必须发送[测试].将A转换为Test易于使用.但是当涉及到Array时,我必须遍历所有数据.有没有什么好方法可以将[A]转换为[Test].