将数组值作为函数swift的参数传递

时间:2017-10-31 06:51:08

标签: arrays swift3.2

我是swift的新手。我试图遵循许多教程,但仍然无法找到它的解决方案。 我想将存储在数组中的值传递给函数。

let trackList = ["AC","BB"]    

for trackId in trackList{

 let ind = trackList.index(of: trackId)
 let index = trackList.startIndex.distance(to: ind!)
 let num = Int(String(describing: ind))
 let track = String(trackId)

 addDiaryList(track:String) //i keep getting error here saying: Cannot convert value of type 'String.Type' to expected argument type 'String'

}

func addDiaryList(track:String) {
}

1 个答案:

答案 0 :(得分:0)

这很简单,函数调用错误。它应该是

addDiaryList(track:track!)

第一个track代表函数中的参数名称,第二个代表上面分配了两行的变量。

但我认为你在这里做了很多无用的电话,它可能是:

let trackList = ["AC","BB"]

func addDiaryList(track:String) {
}

for trackId in trackList{
    addDiaryList(track: trackId)
}